datefield可能不是NULL

时间:2013-08-18 17:35:44

标签: django django-models

我正在尝试从User.profile创建更新表单。但是当我加载网址时出现错误:

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')

2 个答案:

答案 0 :(得分:2)

您可能已经在编辑之前运行了syncdb(请参阅karthikr的评论)。您可以删除表并重新运行syncdb,也可以使用SQL编辑表:

ALTER TABLE user_profile ALTER COLUMN dob DROP NOT NULL;

答案 1 :(得分:1)

我认为您无法使用syncdb

更新现有表格

为了使其正常工作,您应该使用SQL或任何可用的GUI手动编辑数据库,或者您可以删除要编辑的表,使用{{1更新models.py } / null=True然后运行blank=True