Robin
Robin

Reputation: 5486

datefield may not be NULL

I am trying to create an update form from User.profile. But when i load the url i get an error:

IntegrityError at /accounts/profile/
userprofile_userprofile.dob may not be NULL

Even if I give the 'dob' as null=True, I am getting the same error. But from the admin, I can add UserProfiles. Where am I doing wrong? Please help.

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

Upvotes: 0

Views: 1542

Answers (2)

Zacharoo
Zacharoo

Reputation: 113

I don't think you are able to update existing tables with syncdb

In order for this to work you should either manually edit the database using SQL or whatever GUI you have available or you can drop the table(s) that you want to edit, update your models.py with null=True/blank=Trueand then run syncdb.

Upvotes: 1

Joseph Paetz
Joseph Paetz

Reputation: 876

You have likely already run syncdb prior to making your edits (see karthikr's comment). You can either delete the table and rerun syncdb or you can edit the table using SQL:

ALTER TABLE user_profile ALTER COLUMN dob DROP NOT NULL;

Upvotes: 2

Related Questions