无法访问我的管理页面django

时间:2016-03-21 08:09:54

标签: django django-models django-admin

美好的一天,我有两个用户表单,我根据具体情况使用。

我遇到以下错误,但不确定原因:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7fc7c8e48378>
Traceback (most recent call last):
  File "/home/drcongo/.virtualenvs/store/lib/python3.4/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/drcongo/.virtualenvs/store/lib/python3.4/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
    self.check(display_num_errors=True)
  File "/home/drcongo/.virtualenvs/store/lib/python3.4/site-packages/django/core/management/base.py", line 472, in check
    raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
<class 'accounts.admin.UserAdmin'>: (admin.E019) The value of 'filter_horizontal[0]' refers to 'groups', which is not an attribute of 'accounts.StoreOwner'.
<class 'accounts.admin.UserAdmin'>: (admin.E019) The value of 'filter_horizontal[1]' refers to 'user_permissions', which is not an attribute of 'accounts.StoreOwner'.
<class 'accounts.admin.UserAdmin'>: (admin.E108) The value of 'list_display[1]' refers to 'email', which is not a callable, an attribute of 'UserAdmin', or an attribute or method on 'accounts.StoreOwner'.
<class 'accounts.admin.UserAdmin'>: (admin.E108) The value of 'list_display[2]' refers to 'first_name', which is not a callable, an attribute of 'UserAdmin', or an attribute or method on 'accounts.StoreOwner'.
<class 'accounts.admin.UserAdmin'>: (admin.E108) The value of 'list_display[3]' refers to 'last_name', which is not a callable, an attribute of 'UserAdmin', or an attribute or method on 'accounts.StoreOwner'.
<class 'accounts.admin.UserAdmin'>: (admin.E108) The value of 'list_display[4]' refers to 'is_staff', which is not a callable, an attribute of 'UserAdmin', or an attribute or method on 'accounts.StoreOwner'.
<class 'accounts.admin.UserAdmin'>: (admin.E116) The value of 'list_filter[0]' refers to 'is_staff', which does not refer to a Field.
<class 'accounts.admin.UserAdmin'>: (admin.E116) The value of 'list_filter[1]' refers to 'is_superuser', which does not refer to a Field.
<class 'accounts.admin.UserAdmin'>: (admin.E116) The value of 'list_filter[2]' refers to 'is_active', which does not refer to a Field.
<class 'accounts.admin.UserAdmin'>: (admin.E116) The value of 'list_filter[3]' refers to 'groups', which does not refer to a Field.

以下是我的模特:

from django.contrib.auth.models import AbstractUser
from django.db import models

# Create your models here.
from store.models import Category


class PrimaryUser(AbstractUser):
    """
        Model to create, and register users.
    """
    def __str__(self):
        return [self.username]


class StoreOwner(models.Model):
    """
        Model to create, and register store.
    """
    username = models.ForeignKey(PrimaryUser)
    store_name = models.ForeignKey('Store', max_length=50)

    def __str__(self):
        return " ".join([self.username, self.store_name])


class Store(models.Model):
    store_name = models.CharField(max_length=50)
    category = models.ManyToManyField(Category)

并在我的设置中:

AUTH_USER_MODEL = 'accounts.PrimaryUser'

和admin.py:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from .models import StoreOwner

@admin.register(StoreOwner)
class UserAdmin(UserAdmin):

    list_display = list(UserAdmin.list_display) + [
        'store_name',
    ]

和我的表格:

from django import forms
from django.utils.translation import ugettext as _

from .models import StoreOwner, PrimaryUser


class StoreOwnerForm(forms.ModelForm):

    password1 = forms.CharField(label="password", widget=forms.PasswordInput)
    password2 = forms.CharField(label="confirm password", widget=forms.PasswordInput)

    error_messages = {
        'password_mismatch': _('Your password didn\'t match'),
    }

    class Meta:
        model = StoreOwner

        fields = [

            'username',
            'store_name',
            'password1',
            'password2',
        ]

        help_texts = {
            'username': None,
        }

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError(
                self.error_messages['password_mismatch'],
                code='password_mismatch',
            )
        return password2


class PrimaryUserForm(forms.ModelForm):

    password1 = forms.CharField(label="password", widget=forms.PasswordInput)
    password2 = forms.CharField(label="confirm password", widget=forms.PasswordInput)

    error_messages = {
        'password_mismatch': _('Your password didn\'t match'),
    }

    class Meta:
        model = PrimaryUser

        fields = [

            'username',
            'password1',
            'password2',
        ]

        help_texts = {
            'username': None,
        }

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError(
                self.error_messages['password_mismatch'],
                code='password_mismatch',
            )
        return password2

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您的StoreOwner没有AbstractUserPrimaryUser个字段,但您将其注册到用户管理员:

@admin.register(StoreOwner)
class UserAdmin(UserAdmin):

你可以做两件事:

  • StoreOwner注册到普通的ModelAdmin并将用户添加为 内联管理员。请参阅docs

  • StoreOwner扩展PrimaryUser而不是使用主要内容 键。