无法从Admin保存Django Proxy用户模型

时间:2018-04-25 18:33:08

标签: django django-models

保存用户对象时收到以下错误消息。我在互联网上搜索了错误消息,但我找不到任何帮助。可能很明显我错过了,但我找不到任何帮助。

有人可以看看并告诉我这里的错误是什么吗?

文件 admin.py

from django.contrib.auth.admin import UserAdmin

class UserAdmin(UserAdmin):
    list_display = ('username', 'email', 'first_name', 'last_name',
                    'is_active', roles, login)
    list_filter = ('groups',)

    def save_model(self, request, obj, form, change):
        queryset = MyUser.objects.filter(id=obj.id)
        if obj.is_active:
            logger.info('User is marked active')
        elif not obj.is_active:
            logger.info('User is marked inactive')

        obj.save()

    def add_view(self, *args, **kwargs):
        self.inlines = []
        return super(UserAdmin, self).add_view(*args, **kwargs)

    def change_view(self, request, object_id, form_url='', extra_context=None):
        self.inlines = (ProfileInline,)
        return super(UserAdmin, self).change_view(request, object_id, form_url, extra_context) 

档案 models.py

class MyUser(User, URLGenerator):
    objects = models.Manager()  # The default manager.
    safe = SafeUserManager()

    class Meta:
        proxy = True
        ordering = ['username']

    def __unicode__(self):
        return self.get_full_name()

我得到的错误:

Internal Server Error: /admin/public/myuser/1/change/
Traceback (most recent call last):
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 551, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 224, in inner
    return view(request, *args, **kwargs)
  File "/dp/app/dpapp/public/admin.py", line 364, in change_view
    return super(UserAdmin, self).change_view(request, object_id, form_url, extra_context)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1511, in change_view
    return self.changeform_view(request, object_id, form_url, extra_context)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1408, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1440, in _changeform_view
    if form.is_valid():
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/forms/forms.py", line 183, in is_valid
    return self.is_bound and not self.errors
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/forms/forms.py", line 175, in errors
    self.full_clean()
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/forms/forms.py", line 386, in full_clean
    self._post_clean()
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/forms/models.py", line 413, in _post_clean
    self.instance.full_clean(exclude=exclude, validate_unique=False)
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 1235, in full_clean
    self.clean()
  File "/dp/app/venv/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 349, in clean
    self.email = self.__class__.objects.normalize_email(self.email)
AttributeError: 'Manager' object has no attribute 'normalize_email'

1 个答案:

答案 0 :(得分:1)

您的用户对象没有经理方法normalize_email。尝试将用户的经理更改为

objects = BaseUserManager()

您可以在django.contrib.auth.base_user

中导入