如何在Django中创建datetime2模型

huangapple go评论64阅读模式
英文:

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.
&gt; [https://stackoverflow.com/questions/65600327/sql-datetime27-field-to-django](https://stackoverflow.com) 
&gt; 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>



huangapple
  • 本文由 发表于 2023年7月6日 20:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定