0

我正在尝试从 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')
4

2 回答 2

2

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

ALTER TABLE user_profile ALTER COLUMN dob DROP NOT NULL;
于 2013-08-18T19:05:40.200 回答
1

我认为您无法使用syncdb

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

于 2013-08-18T19:35:34.517 回答