【问题标题】:Django - User creation with custom user model results in internal errorDjango - 使用自定义用户模型创建用户会导致内部错误
【发布时间】:2013-01-29 05:52:55
【问题描述】:

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

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

感谢您的帮助

【问题讨论】:

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


    【解决方案1】:
    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-12-23
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 2020-08-22
    • 2017-07-13
    相关资源
    最近更新 更多