admin 中的 Django 自定义用户模型,关系“auth_user";不存在 [英] Django custom user model in admin, relation "auth_user" does not exist

查看:24
本文介绍了admin 中的 Django 自定义用户模型,关系“auth_user";不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义用户模型,如下所示:

I have a custom user model as below:

class User(AbstractUser):
    subscribe_newsletters = models.BooleanField(default=True)
    old_id = models.IntegerField(null=True, blank=True)
    old_source = models.CharField(max_length=25, null=True, blank=True)

并使用内置的 UserAdmin

And using the builtin UserAdmin

admin.site.register(User, UserAdmin)

虽然编辑用户记录工作正常,但当我添加用户时,出现以下错误

While editing the user record works fine, but when I add a user, I get the following error

Exception Value: 
relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...

推荐答案

经过一番挖掘,我发现了这个

After some digging around I found this

https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#custom-users-and-the-built-in-auth-forms

罪魁祸首是django.contrib.auth.forms.pyUserCreationForm内的一个函数clean_username.已经创建了一些票证,但显然维护人员认为这不是缺陷:

The culprit is a function clean_username inside UserCreationForm inside django.contrib.auth.forms.py. A few tickets have been created, but apparently the maintainers don't think it's a defect:

https://code.djangoproject.com/ticket/20188

https://code.djangoproject.com/ticket/20086

def clean_username(self):
        # Since User.username is unique, this check is redundant,
        # but it sets a nicer error message than the ORM. See #13147.
        username = self.cleaned_data["username"]
        try:
            User._default_manager.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError(self.error_messages['duplicate_username'])

这个文件中的User直接引用了内置的用户模型.

The User in this file is directly referencing to the builtin user model.

为了解决这个问题,我创建了自定义表单

To fix it, I created my custom forms

from models import User #you can use get_user_model
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth import forms

class MyUserCreationForm(UserCreationForm):
    def clean_username(self):
        # Since User.username is unique, this check is redundant,
        # but it sets a nicer error message than the ORM. See #13147.
        username = self.cleaned_data["username"]
        try:
            User._default_manager.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError(self.error_messages['duplicate_username'])

    class Meta(UserCreationForm.Meta):
        model = User

class MyUserAdmin(UserAdmin):  
    add_form = MyUserCreationForm   

admin.site.register(User,MyUserAdmin)

或者你可以尝试用猴子修补原始的UserCreationForm 来替换User 变量.

Or you can try monkey patching the original UserCreationForm to replace the User variable.

这篇关于admin 中的 Django 自定义用户模型,关系“auth_user";不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆