我想從User.profile創建更新表單。但是,當我加載網址我得到一個錯誤:datefield可能不是NULL
IntegrityError at /accounts/profile/
userprofile_userprofile.dob may not be NULL
即使我給「DOB」爲null=True
,我收到了同樣的錯誤。但是從管理員,我可以添加UserProfiles。我在哪裏做錯了?請幫忙。
models.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])
forms.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')
是否添加了'null = True,blank = True'約束_after_正在運行syncdb? – karthikr