我正在尝试从 User.profile 创建一个更新表单。但是当我加载 url 时出现错误:
IntegrityError at /accounts/profile/
userprofile_userprofile.dob may not be NULL
即使我给'dob' as null=True
,我也会遇到同样的错误。但是从管理员那里,我可以添加用户配置文件。我在哪里做错了?请帮忙。
模型.py:
from django.db import models
from django.contrib.auth.models import User
from time import time
def get_upload_file_name(instance, filename):
return "uploaded_files/profile/%s_%s" %(str(time()).replace('.','_'), filename)
class UserProfile(models.Model):
GENDER = (
("M", "Male"),
("F", "Female"),
("O", "Other"),
)
RELATIONSHIP = (
("S", "Single"),
("M", "Married"),
("D", "Divorced"),
)
user = models.OneToOneField(User)
image = models.ImageField(upload_to=get_upload_file_name)
gender = models.CharField(max_length=1, choices=GENDER)
dob = models.DateField('date of birth', blank=True, null=True)
about = models.TextField()
occupation = models.CharField(max_length=30)
state = models.CharField(max_length=20)
t_address = models.CharField(max_length=30)
p_address = models.CharField(max_length=30)
relationship = models.CharField(max_length=1, choices=RELATIONSHIP)
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
表格.py:
from django import forms
from models import UserProfile
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('image', 'gender', 'dob', 'about', 'state', 't_address', 'p_address', 'relationship')