【发布时间】:2013-04-29 10:13:17
【问题描述】:
我正在尝试在 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我确定“主文件夹”在pythonpath上。应用中的 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.一个新线索我不知道怎么解读..
【问题讨论】:
-
你的应用名称是
'app'吗? -
首先,不要惊慌,我们会解决这个问题的。 :) 你什么时候看到错误?如果禁用管理员,错误会消失吗?你试过一步步调试吗?
-
@Ivan 好的,很高兴听到 :) 在 /admin 上输入用户凭据并按“登录”后会发生错误。如果我关闭管理员,我希望错误会消失,现在没有机会测试,但是与管理员无关的页面可以正常工作。
-
@Rohan 不,我在发帖时出于匿名目的稍微更改了代码,可能有点偏执,但无论如何。
-
我认为我们可以将问题缩小到您的
MyUserAdmin表单。当您提交问题时,您的AUTH_USER_MODEL已正确声明。你试过this fix吗?
标签: django django-models django-admin django-1.5