英文:
How can i create datetime2 model on Django
问题
我对这个主题进行了一些研究,但我找不到解决方案。
```python
CreatedDate=models.DateTimeField(auto_now_add=True)
当我使用这段代码创建模型时,mssql的数据类型被设置为datetimeoffset(7)。但我希望创建为datetime2(7)类型。
我尝试了django-mssql-backend库(它将其设置为datetimeoffset(7))。我期望得到datetime2。
> https://stackoverflow.com/questions/65600327/sql-datetime27-field-to-django
> 我按照这里的说明,但解决方案对我不起作用。
简而言之,我如何在使用Django模型时创建datetime2?
<details>
<summary>英文:</summary>
I do some research about this topic but i cannot find a solution.
CreatedDate=models.DateTimeField(auto_now_add=True)
when i create a model using this code, data type of mssql set as datetimeoffset(7).But i wish to create as type of datetime2(7).
I tried django-mssql-backend library.(It set as datetimeoffset(7)).I was expected datetime2.
> [https://stackoverflow.com/questions/65600327/sql-datetime27-field-to-django](https://stackoverflow.com)
> I followed the instructions here but the solution did not work for me
Shortly, how can i create datetime2 with using Django model?
</details>
# 答案1
**得分**: 0
问题与时区相关。解决方法如下:
- 上传 [mssql-django][1]
- 在Django设置中将时区设置为 `USE_TZ = False`,模型创建为datetime2。
[1]: https://github.com/microsoft/mssql-django
<details>
<summary>英文:</summary>
The problem was releated with timezone.To solution:
- Upload [mssql-django][1]
- Set timezone ( `USE_TZ = False` ) as false in django settings, model created as datetime2.
[1]: https://github.com/microsoft/mssql-django
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论