2016-03-21 37 views
0

美好的一天,我有两个用户表单,我根据具体情况使用。无法访问我的管理页面django

我有以下错误,但不知道为什么:

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' 

和管理.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 

将非常感谢所有帮助

回答

0

StoreOwner没有任何AbstractUserPrimaryUser领域,但你把它注册到用户管理:

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

你可以做两件事:

  • StoreOwner注册到正常的ModelAdmin并将用户添加为 内嵌管理员。见docs

  • StoreOwner延长PrimaryUser而不是使用主键 键。

+0

我不知道要明白你说什么,你可以请@sandervanleeuwen –

+0

更新答案.. –

+0

更明确的使用你的方法,我有这样的错误:OperationalError没有这样的列:accounts_storeowner.primaryuser_ptr_id –