id may not be NULL, django user registration
I am trying to register users using the Django UserProfile capability. I have an Extended UserProfile model. My files look like below. I am getting "accounts_userprofile.u_id may not be NULL', i tried using 'commit=false' when saving the form, but it didn't work. My parent model has the fields set to null= True, blank = True. I have dropped the table and added it a couple of times, but nothing worked. Please help. Thanks in advance
My Models are below:
class UserBase(models.Model):
class Meta:
abstract = True
u_id = models.IntegerField(null = True,blank = True)
email = models.CharField(max_length=200, null = True,blank = True)
website=models.CharField(max_length=200, null = True, blank = True)
age = models.IntegerField(blank = True, null = True)
location = models.CharField(max_length= 200, null=True, blank = True)
username = models.CharField(max_length = 100, null = True, blank = True)
The UserProfile Model is as follows:
class UserProfile(UserBase):
user = models.ForeignKey(User, null= True, blank = True, unique = True)
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
def __unicode__(self):
return "User Profile for: " + self.user.username
def create_user_profile(sender, **kw): user = kw['instance'] if kw['created']: up = UserProfile(user=user)
up.save()
post_save.connect(create_user_profile, sender = User, dispatch_uid ="user_create_profile")
My registration View is below:
def register(request, template_name="registration/register.html"): if request.method == 'POST':
postdata = request.POST.copy()
form = UserCreationForm(postdata)
if form.is_valid():
un = postdata.get('username','')
pw = postdata.get('password1','')
from django.contrib.auth import login, authenticate
new_user = authenticate(username=un, password=pw)
form.save()
#if new_user and new_user.is_active:
#login(request, new_user)
#url = urlresolvers.reverse('my_account')
#return HttpResponseRedirect(url)
else: form = UserCreationForm() page_title = 'User Registration' return render_to_response(template_name, locals(), context_instance=RequestContext(request))>
From Django Docs:
https://docs.djangoproject.com/en/1.4/ref/models/fields/
Field.unique
If True, this field must be unique throughout the table.
This is enforced at the database level and by model validation. If you try to save a model with a duplicate value in a unique field, a django.db.IntegrityError will be raised by the model's save() method.
This option is valid on all field types except ManyToManyField and FileField.
You are telling UserProfiles that the UserProfiles.u_id is unique, it's not (blanks/null). So you're getting the error. You may want to consider changing u_id to be a AutoField primary_key. And then connect them using models.ForeignKey('UserBase')
链接地址: http://www.djcxy.com/p/9680.html