Django loaddata抛出ValidationError:[u'输入YYYY-MM-DD格式的有效日期。']在null = true字段?

时间:2010-04-25 06:29:15

标签: django django-admin

当我跑步时:

   django-admin.py loaddata ../data/library_authors.json 

错误是:

   ...
   ValidationError: [u'Enter a valid date in YYYY-MM-DD format.']

模特:

   class Writer(models.Model):
     first = models.CharField(u'First Name', max_length=30)
     other = models.CharField(u'Other Names', max_length=30, blank=True)
     last = models.CharField(u'Last Name', max_length=30)
     dob = models.DateField(u'Date of Birth', blank=True, null=True)

     class Meta:
       abstract = True
       ordering = ['last']
       unique_together = ("first", "last")

   class Author(Writer):
     language = models.CharField(max_length=20, choices=LANGUAGES, blank=True)

     class Meta:
       verbose_name = 'Author'
       verbose_name_plural = 'Authors'

请注意,dob DateField的空白= True,null = True

json文件具有结构:

[
    {
        "pk": 1, 
        "model": "books.author", 
        "fields": {
            "dob": "", 
            "other": "", 
            "last": "Carey", 
            "language": "", 
            "first": "Peter"
        }
    }, 
    {
        "pk": 3, 
        "model": "books.author", 
        "fields": {
            "dob": "", 
            "other": "", 
            "last": "Brown", 
            "language": "", 
            "first": "Carter"
        }
    }
]

支持mysql数据库将相关表中的相关日期字段设置为NULL作为默认值并且为空? =是。

关于我做错了什么或者如何让loaddata接受null日期值的任何想法?

===== 编辑:

Daniel的回答有效,我是红脸。

1 个答案:

答案 0 :(得分:2)

只需将""的{​​{1}}更改为dob即可,但空字符串与null不同。即使你有null,这只是表单验证而不是通过灯具进行输入验证。