我正在尝试在Django中设置我的自定义用户模型。原因是我想使用电子邮件作为用户名,并完全删除用户名字段。我遇到了一个错误,我无法弄明白。
Manager isn't available; User has been swapped for 'app.MyUser'
Exception Location: .../django/db/models/manager.py in __get__, line 256
Python Version: 2.7.3
Python Path:
[...project specific files,
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PIL',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
我已经像疯了一样搜索,但是没有找到关于此错误消息的太多页面。我找到了一些页面,并提出了如何解决它的建议,但没有一个建议对我有用。
我的代码:我已经设置了自定义用户模型。我在settings.py中声明了自定义用户模型AUTH_USER_MODEL = 'app.MyUser'
。我还设置了一个自定义UserManager:
class MyUserManager(BaseUserManager):
def create_user(self, email, password=None):
"""
Creates and saves a User with the given email. Note that none of the optional fields gets values in the creation. These fields will have to be filled out later on.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(email=MyUserManager.normalize_email(email))
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password=None):
"""
Creates and saves a superuser with the the above mentioned attributes
"""
user = self.create_user(email, password=password)
user.is_admin = True
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser, PermissionsMixin):
"""
Custom made User model. No username, instead email is used as unique field and index
"""
Genders = (('M', 'Man'), ('K', 'Woman'))
FirstName = models.CharField(max_length=30)
LastName = models.CharField(max_length=40)
Gender = models.CharField(max_length=2, choices=Genders, default='K')
email = models.EmailField(verbose_name='email address', max_length=255, unique=True, db_index=True,)
twitter = models.CharField(max_length=30)
is_admin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def get_full_name(self):
# The user is identified by their email address
return self.email
def get_short_name(self):
# The user is identified by their email address
return self.email
def __unicode__(self):
return self.email
objects = MyUserManager()
我试图向不同类型的UserAdmins声明,其中没有任何区别,我试过的第一个;
class MyUserAdmin(UserAdmin):
# The forms to add and change user instances
#form = UserChangeForm
#add_form = FrontpageRegistrationForm
list_display = ('email', 'FirstName', 'LastName', 'Gender', 'twitter')
list_filter = ()
add_fieldsets = ((None, {'classes': ('wide',),'fields': ('email', 'password1', 'password2')}),)
search_fields = ('email',)
ordering = ('email',)
filter_horizontal = ()
admin.site.register(MyUser, MyUserAdmin)
我已经注释掉了add_form
和form
这两个属性,因为它们引发了一些我希望稍后再回到的表单错误。
在阅读了可能的修复here后,第二个UserAdmin已经制作完成。但这对情况没有帮助;
class MyUserAdmin(admin.ModelAdmin):
# The forms to add and change user instances
#form = UserChangeForm
add_form = FrontpageRegistrationForm
add_fieldsets = ((None, {'classes': ('wide',),'fields': ('email', 'password1', 'password2')}),)
def get_fieldsets(self, request, obj=None):
if not obj:
return self.add_fieldsets
return super(MyUserAdmin, self).get_fieldsets(request, obj)
def get_form(self, request, obj=None, **kwargs):
defaults = {}
if obj is None:
defaults.update({'form': self.add_form,'fields': admin.util.flatten_fieldsets(self.add_fieldsets),})
defaults.update(kwargs)
return super(MyUserAdmin, self).get_form(request, obj, **defaults)
我也尝试删除数据库中的所有表,但没有运气。
对于那些甚至关注这个问题的人来说,我永远都会感到非常高兴。如果有人要解决这个问题,我会尽我所能地告诉我的妻子在阿凡达之后命名我们的长子给我一个解决方案,这样我就可以继续过我的生活。
编辑:
我尝试将AUTH_USER_MODEL
设置为mainfolder.app.MyUser
我确定“mainfolder”位于python路径上。应用中的 init .py应该是正确的。新的settings.py给出了以下服务器错误; auth.user: AUTH_USER_MODEL is not of the form 'app_label.app_name'.admin.logentry: 'user' has a relation with model smartflightsearch.SFSdb.MyUser, which has either not been installed or is abstract.registration.registrationprofile: 'user' has a relation with model, which has either not been installed or is abstract.
一条新线索,我不知道如何解释..
答案 0 :(得分:9)
TL; DR:在以下答案结尾处使用 Solution 部分中的代码。
更长的解释:你知道,从 Django 1.5 开始,仅仅将Django的UserAdmin
子类化为能够与可交换用户模型交互:您还需要覆盖相应的表单。
如果您跳转到django.contrib.auth.admin
source,您会看到UserAdmin's
form
和add_form
包含以下值:
# django/contrib/auth/admin.py
class UserAdmin(admin.ModelAdmin):
...
form = UserChangeForm
add_form = UserCreationForm
我们forms in django.contrib.auth.forms
指出不尊重可插拔用户模型:
# django/contrib/auth/forms.py
class UserCreationForm(forms.ModelForm):
...
class Meta:
model = User # non-swappable User model here.
class UserChangeForm(forms.ModelForm):
...
class Meta:
model = User # non-swappable User model here.
解决方案:所以,您应该关注已有的answer(不要忘记投票!),其归结为:
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
class MyUserChangeForm(UserChangeForm):
class Meta:
model = get_user_model()
class MyUserCreationForm(UserCreationForm):
class Meta:
model = get_user_model()
class MyUserAdmin(UserAdmin):
form = MyUserChangeForm
add_form = MyUserCreationForm
admin.site.register(MyUser, MyUserAdmin)
希望这将在Django的未来版本中修复(这里是bug跟踪器中的corresponding ticket)。
答案 1 :(得分:2)
当你说你设置AUTH_USER_MODEL = 'app.MyUser'
我假设你的应用位于MyUser
类时,有一个结构,perharps,像这样:
所以在models.py中你有MyUser
并且在 init .py:
from models import MyUser