好的,我知道这是一个愚蠢的问题,但我被阻止了,我不知道该怎么做。
我在 google 和 stackoverflow 上搜索过,但没有找到任何答案:我试过这个:
- 在 django 中向用户添加自定义字段
- Django - 在用户创建时创建用户配置文件
- https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
我的模型如下:
class UserProfile(models.Model):
user = models.OneToOneField(User)
quota = models.IntegerField(null = True)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
我对用户注册的看法如下:
def register(request):
if request.method == 'POST': # If the form has been submitted...
form = RegistrationForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
cd = form.cleaned_data
#Then we create the user
user = User.objects.create_user(cd['username'],cd["email"],cd["password1"])
user.get_profil().quota = 20
user.save()
return HttpResponseRedirect('')
else:
form = RegistrationForm() # An unbound form
return render(request, 'registration_form.html', {'form': form,})
启动 InternalError 的行是:
user = User.objects.create_user(cd['username'],cd["email"],cd["password1"])
错误是:
InternalError at /register/
current transaction is aborted, commands ignored until end of transaction block
谢谢您的帮助