我正在尝试使用 Django UserProfile 功能注册用户。我有一个扩展的 UserProfile 模型。我的文件如下所示。我收到“accounts_userprofile.u_id 可能不是 NULL”,我在保存表单时尝试使用 'commit=false',但它不起作用。我的父模型将字段设置为 null = True,blank = True。我已经删除了表格并添加了几次,但没有任何效果。请帮助。提前致谢
我的模型如下:
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)
UserProfile 模型如下:
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")
我的注册视图如下:
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 = '用户注册' return render_to_response(template_name, locals(), context_instance=RequestContext(request))>