我尝试保存用户的出生日期,但得到“列“dob”中的空值违反非空约束”错误。
模型.py:
class Profile(models.Model):
user = models.OneToOneField(User, unique=True)
nickname = models.CharField(max_length=32)
dob = models.DateField(null=False)
sex = models.BooleanField(null=False)
在这里我尝试生成随机用户:
def create_random_users(userCount=1000):
random.seed()
for i in range(0, userCount):
sex = random.randint(0, 1)
name = random.choice(names[sex])
email = "{0}{1}@mail.com".format(name, i)
user = soc_models.User.objects.create_user(email, email, password='password')
user.save()
userProfile = soc_models.Profile.objects.create()
userProfile.user = user
_year = random.randrange(1962, 1995)
_month = random.randrange(1, 12)
_day = random.randrange(1, calendar.monthrange(_year, _month)[1])
userProfile.dob = datetime.datetime(_year, _month, _day)
userProfile.sex = random.randrange(0, 1)
userProfile.city = random.randrange(4000000)
userProfile.country = random.randrange(230)
userProfile.save()
谢谢你。