Django - 使用自定义用户模型创建用户会导致内部错误

时间:2013-02-13 16:04:46

标签: python django django-models django-users django-managers

好的,我知道这是一个愚蠢的问题,但我被封锁了,我无法弄清楚该怎么做。

我搜索了google和stackoverflow但没有找到任何答案: 我试过这个:

我的模型如下:

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

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

user = User.objects.create_user(username=form.cleaned_data['username'],
                                password=form.cleaned_data['password'], 
                                email=form.cleaned_data['email'])
user.is_active = True
user.save()