1

好的,我知道这是一个愚蠢的问题,但我被阻止了,我不知道该怎么做。

我在 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

谢谢您的帮助

4

1 回答 1

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()
于 2013-02-13T16:23:14.187 回答