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)
And using the builtin UserAdmin
使用内置的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...
4 个解决方案
#1
27
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
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:
罪魁祸首是一个函数clean_username在django. scheme . authb .forms.py内部的UserCreationForm中。已经有几张票被创造出来了,但显然维护者并不认为这是一个缺陷:
https://code.djangoproject.com/ticket/20188
https://code.djangoproject.com/ticket/20188
https://code.djangoproject.com/ticket/20086
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'])
The User
in this file is directly referencing to the builtin user model.
该文件中的用户直接引用builtin用户模型。
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)
Or you can try monkey patching the original UserCreationForm
to replace the User
variable.
或者您可以尝试修改原始的UserCreationForm来替换User变量。
#2
4
This is due to migration is not run. This issue is resolved for me by running following command:
这是因为迁移没有运行。这个问题通过以下命令来解决:
python manage.py syncdb
python管理。py syncdb
#3
3
Django 1.8
Django 1.8
If your app is not yet using migrations then this could also be the problem, as contrib.auth uses them. Enabling migrations for my app solved it for me.
如果你的应用程序还没有使用迁移,那么这也可能是问题所在。身份验证使用它们。我的应用程序的迁移为我解决了这个问题。
$ ./manage.py makemigrations <my_app>
$ ./manage.py migrate
#4
0
Migrate your app (the one with custom user model) first, and only then the rest:
首先迁移你的应用程序(有自定义用户模型的那个),然后再迁移其他的:
$ ./manage.py makemigrations <your_app>
$ ./manage.py migrate
$ ./manage.py makemigrations
$ ./manage.py migrate
You can also control the order of migrations to make sure this happens automatically, see https://docs.djangoproject.com/en/1.10/howto/writing-migrations/#controlling-the-order-of-migrations
您还可以控制迁移的顺序,以确保这是自动发生的,请参见https://docs.djangoproject.com/en/1.10/howto/writing-migrations/#controlling-the- of migration
#1
27
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
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:
罪魁祸首是一个函数clean_username在django. scheme . authb .forms.py内部的UserCreationForm中。已经有几张票被创造出来了,但显然维护者并不认为这是一个缺陷:
https://code.djangoproject.com/ticket/20188
https://code.djangoproject.com/ticket/20188
https://code.djangoproject.com/ticket/20086
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'])
The User
in this file is directly referencing to the builtin user model.
该文件中的用户直接引用builtin用户模型。
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)
Or you can try monkey patching the original UserCreationForm
to replace the User
variable.
或者您可以尝试修改原始的UserCreationForm来替换User变量。
#2
4
This is due to migration is not run. This issue is resolved for me by running following command:
这是因为迁移没有运行。这个问题通过以下命令来解决:
python manage.py syncdb
python管理。py syncdb
#3
3
Django 1.8
Django 1.8
If your app is not yet using migrations then this could also be the problem, as contrib.auth uses them. Enabling migrations for my app solved it for me.
如果你的应用程序还没有使用迁移,那么这也可能是问题所在。身份验证使用它们。我的应用程序的迁移为我解决了这个问题。
$ ./manage.py makemigrations <my_app>
$ ./manage.py migrate
#4
0
Migrate your app (the one with custom user model) first, and only then the rest:
首先迁移你的应用程序(有自定义用户模型的那个),然后再迁移其他的:
$ ./manage.py makemigrations <your_app>
$ ./manage.py migrate
$ ./manage.py makemigrations
$ ./manage.py migrate
You can also control the order of migrations to make sure this happens automatically, see https://docs.djangoproject.com/en/1.10/howto/writing-migrations/#controlling-the-order-of-migrations
您还可以控制迁移的顺序,以确保这是自动发生的,请参见https://docs.djangoproject.com/en/1.10/howto/writing-migrations/#controlling-the- of migration