我被 models.DateField() 中的一个错误困住了。
首先,我做了这个。
models.py
然后,我得到了,
所以,我做了,
models.py
然后,
这个错误的意思是“''不是一个有效的日期格式。你需要改成YYYY-MM-DD”。
那我应该如何修改这段代码呢? 谢谢。
另外,如果可以的话,我不想把日期插入到birthdate字段中。但好像我必须得插入。我能让它保持空白吗?
未能工作。
Python 3.5.1 Django 1.9.1
首先,我做了这个。
models.py
from datetime import date, datetime
from django.db import models
class User(models.Model):
uid = models.AutoField(primary_key=True)
birthdate = models.DateField()
然后,我得到了,
$ python manage.py makemigrations
You are trying to add a non-nullable field 'birthdate' to user_profile without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
所以,我做了,
models.py
from datetime import date, datetime
from django.db import models
class User(models.Model):
uid = models.AutoField(primary_key=True)
birthdate = models.DateField(default=date.today)
然后,
$ python manage.py migrate
django.core.exceptions.ValidationError: ["'' は無効な日付形式です。YYYY-MM-DD形式にしなければなりません。"]
这个错误的意思是“''不是一个有效的日期格式。你需要改成YYYY-MM-DD”。
那我应该如何修改这段代码呢? 谢谢。
另外,如果可以的话,我不想把日期插入到birthdate字段中。但好像我必须得插入。我能让它保持空白吗?
birthdate = models.DateField(null=True, blank=False)
未能工作。
Python 3.5.1 Django 1.9.1
blank=False
表示该字段不能为空。您可以将其更改为blank=True
,然后重试吗? - Shang Wangnull=True, blank=True
时有选择的提示,那么第二个选项难道不是你想要的(将字段保留为空)吗?此外,我感觉你的迁移可能出了问题,因为你尝试了多次迁移。你应该删除负责此操作的迁移文件并重新生成。 - Shang Wang