Django loaddata 抛出 ValidationError: [u'Enter a valid date in YYYY-MM-DD format.'] on null=true 字段?
当我运行时:
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 作为默认值和 Null ? = 是的。
关于我做错了什么或者如何让 loaddata 接受空日期值有什么想法吗?
===== 编辑:
丹尼尔的回答有效,我脸红了。
When I run:
django-admin.py loaddata ../data/library_authors.json
the error is:
...
ValidationError: [u'Enter a valid date in YYYY-MM-DD format.']
The model:
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'
Note that the dob DateField has blank=True, null=True
The json file has structure:
[
{
"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"
}
}
]
The backing mysql database has the relevent date field in the relevant table set to NULL as default and Null? = YES.
Any ideas on what I'm doing wrong or how I can get loaddata to accept null date values?
=====
EDIT:
Daniel's answer worked, I'm redfaced.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将
dob
的""
更改为null
即可,空字符串与null
不同代码>.即使您在那里有blank=True
,但这实际上仅用于表单验证,而不是通过固定装置进行输入验证。Simply change the
""
fordob
tonull
and it will work, the empty string isn't the same asnull
. Even though you haveblank=True
in there, that's really only for form validation and not input validation through fixtures.