Django loaddata 抛出 ValidationError: [u'Enter a valid date in YYYY-MM-DD format.'] on null=true 字段?

发布于 2024-08-30 16:28:18 字数 1554 浏览 3 评论 0原文

当我运行时:

   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2024-09-06 16:28:18

只需将 dob"" 更改为 null 即可,空字符串与 null 不同代码>.即使您在那里有 blank=True ,但这实际上仅用于表单验证,而不是通过固定装置进行输入验证。

Simply change the "" for dob to null and it will work, the empty string isn't the same as null. Even though you have blank=True in there, that's really only for form validation and not input validation through fixtures.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文