为PoUser指定的Django未知字段(用户名)

时间:2013-06-19 16:15:46

标签: django model admin createuser

我收到此错误是因为我创建了一个自定义用户模型,当我得到所有设置时,我查看了django / contrib / auth / models.py,我将电子邮件设置为USERNAME_FIELD,而REQUIRED_FIELD是一些字段,在我的UserModel继承的AbstractBaseUser和PermissionMixin中没有定义,我可以访问除“Po User add section”之外的所有部分,并且我在models.py和admin.py以及settings.py,urls.py中输入我的代码,希望有人有帮助。非常感谢你

错误痕迹:

Unknown field(s) (username) specified for PoUser
Request Method: GET
Request URL:    http://127.0.0.1/newproject/admin/Testsite/pouser/add/
Django Version: 1.5.1
Exception Type: FieldError
Exception Value:    
Unknown field(s) (username) specified for PoUser
Exception Location: /usr/local/lib/python2.7/dist-packages/django/forms/models.py in __new__, line 221
Python Executable:  /usr/bin/python
Python Version: 2.7.3
/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response
                        response = callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py in wrapper
                return self.admin_site.admin_view(view)(*args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py in _wrapped_view
                    response = view_func(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py in _wrapped_view_func
        response = view_func(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py in inner
            return view(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/views/decorators/debug.py in sensitive_post_parameters_wrapper
            return view(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py in _wrapper
            return bound_func(*args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py in _wrapped_view
                    response = view_func(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py in bound_func
                return func(self, *args2, **kwargs2) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/transaction.py in inner
                return func(*args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/admin.py in add_view
                                               extra_context) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py in _wrapper
            return bound_func(*args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py in _wrapped_view
                    response = view_func(request, *args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py in bound_func
                return func(self, *args2, **kwargs2) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/db/transaction.py in inner
                return func(*args, **kwargs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py in add_view
        ModelForm = self.get_form(request) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/admin.py in get_form
        return super(UserAdmin, self).get_form(request, obj, **defaults) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py in get_form
        return modelform_factory(self.model, **defaults) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/forms/models.py in modelform_factory
    return type(form)(class_name, (form,), form_class_attrs) ...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/forms/models.py in __new__
                raise FieldError(message) ...

models.py

from django.db import models
from django.contrib.auth.models import Group
from django.utils import timezone
from django.contrib.auth.models import BaseUserManager,AbstractBaseUser,PermissionsMixin
from django.db import models
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django import forms
from django.contrib import admin
from taggit.managers import TaggableManager
from django.core.exceptions import ValidationError
from django.forms import ModelForm
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
# Create your models here.
class poUserManager(BaseUserManager):
    def create_user(self,email,password=None,first_name=None,last_name=None,last_login=None,date_join=None):
        now=timezone.now()
        if not email:
            raise ValueError("The given email must be setted")
        user = self.model(email=self.normalize_email(email),is_staff=True, is_active=True, is_superuser=False,last_login=now, date_join=date_join,first_name=first_name,last_name=last_name)
        user.set_password(password)
        user.save(using=self._db)
        return user
    def create_superuser(self,email,password,first_name,last_name,last_login,date_join):
        now=timezone.now()
        u=self.create_user(email,password,first_name=first_name,last_name=last_name,date_join=date_join)
        u.is_staff=True
        u.active=True
        u.is_superuser=True
        u.save(using=self.db)
        return u
#@python_2_unicode_compatible
class PoUser(AbstractBaseUser,PermissionsMixin):
    email=models.EmailField(verbose_name='email address',max_length=255,unique=True,db_index=True)
    first_name=models.CharField(max_length=30)
    last_name=models.CharField(max_length=20)


    date_join=models.DateField(default=timezone.now)
    is_staff = models.BooleanField(_('staff status'), default=False,help_text=_('Designates whether the user can log into this admin ''site.'))
    is_active = models.BooleanField(_('active'), default=True,help_text=_('Designates whether this user should be treated as ''active. Unselect this instead of deleting accounts.'))
    is_admin=models.BooleanField(default=False)
    USERNAME_FIELD='email'
    #REQUIRED_FIELD=['first_name','last_name','date_join','is_active','is_admin','groups','user_permissions','is_superuser','last_login','is_staff'] 
    REQUIRED_FIELD=['last_name','first_name','date_join']
    objects=poUserManager()
    def get_full_name(self):
        return self.firstname
    def get_short_name(self):
        return self.first_name
   # def has_perm(self,perm,obj):

    #    if self.is_active and self.is_superuser:
     #       return True
        # return this.groups.values_list(self.email,flat=True)
      #  return _user_has_perm(self,perm.obj)
    def __unicode__(self):
        return self.email
    def get_group(self):
        return self.objects.values('groups')

admin.py:

from django.db import models
from django.contrib.auth.models import Group
from django.utils import timezone
from django.contrib.auth.models import BaseUserManager,AbstractBaseUser,PermissionsMixin
from django.db import models
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django import forms
from Testsite.models import PoUser
from django.contrib import admin
from taggit.managers import TaggableManager
from django.core.exceptions import ValidationError
from django.forms import ModelForm
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.admin import GroupAdmin
from django.contrib import admin

class UserCreationForm(forms.ModelForm):
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
    class Meta:
        model = PoUser
        fields = ('email','first_name','last_name','groups','user_permissions','date_join','is_staff','is_active','is_admin',)
        #fields=('email','first_name','last_name')
    def clean_password2(self):
                # Check that the two password entries match
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords don't match")
        return password2
    def save(self,commit=True):
        user=super(UserCreationForm,self).save(commit=False)
        user.set_password(self.clean_data['password1'])
        if commit:
            user.save()
        return user
class UserChangeForm(forms.ModelForm):
    password=ReadOnlyPasswordHashField()
    class Meta:
        model = PoUser
        fields=['email','first_name','last_name','groups','user_permissions','date_join','is_staff','is_active','is_admin']
    def clean_password(self):
        return self.initial["password"]
class MyUserAdmin(UserAdmin):
    form=UserChangeForm
    add_form=UserCreationForm
    #fields=('email','first_name','last_name','groups','user_permissions','date_join','is_staff','is_admin')
    list_display = ('email','first_name','last_name')
    list_filter=('is_admin',)
    search_fields = ('email','first_name','last_name')
    ordering = ('email',)
    fieldsets = ((None, {'fields': ('email', 'password')}),('Personal info', {'fields': ('last_name','groups','date_join')}),('Permissions', {'fields': ('is_admin','is_staff')}),)

admin.site.register(PoUser, MyUserAdmin)

settings.py:

ADMIN_MEDIA_PREFIX='/admin_media/'
AUTH_USER_MODEL = 'Testsite.PoUser'

1 个答案:

答案 0 :(得分:12)

父类(django.contrib.auth.admin.UserAdmin)具有add_fieldsets属性,其中包含username字段。在名为MyUserAdmin的{​​{1}}类中添加一个属性,并将其视为类似于add_fieldsets属性 - 它定义了您要在添加表单中显示的字段。

有关示例,请参阅源here