2011-03-20 91 views
7

我有一個DateTimeField字段在我的Django的車型之一。Django的DateTimeField字段定義爲空白=真,空=真,但不允許空

completed_date = models.DateTimeField('date completed', blank=True, null=True) 

我已經定義它允許空白和空值。然而,當我嘗試創建模型的實例,我得到以下錯誤:

IntegrityError at /admin/tasks/project/add/

tasks_project.completed_date may not be NULL

我使用Django 1.25和Python 2.7。任何人都知道這是爲什麼發生?有什麼我可以解決這個問題嗎?

我發現了一個ticket that describes the same problem,但它關閉在4年前固定的,所以我想它一定是現在集成到Django的!

+1

你加空=做創建該領域的執行syncdb後,是真的嗎? – Tiago 2011-03-20 12:06:39

+0

是的。但我再次運行syncdb。 – 2011-03-20 12:18:59

+0

除非您刪除運行syncdb的表不會更改字段上的約束。您需要使用遷移工具(如南)或刪除該表,然後再次運行syncdb。 – DTing 2011-03-20 12:30:01

回答

9

django syncdb and an updated model

從提問/回答:

Django doesn't support migrations out of the box. There is a pluggable app for Django that does exactly that though, and it works great. It's called South.

http://south.aeracode.org/

Havent used django in a while, but i seem to remember that syncdb does perform alter commands on db tables. you have to drop the table then run again and it will create again.

+2

我第二南。這是非常快,易於安裝和起牀,並運行它。最好的部分:沒有寫入遷移文件(到目前爲止)。 – Marcin 2011-05-28 16:21:42

相關問題