Django Unknown field(s) (username) specified for PoUser

moojun picture moojun · Jun 19, 2013 · Viewed 16.9k times · Source

I got this error because i create a custom usermodel, and When i got all setup, which i look into the django/contrib/auth/models.py, i set the email as USERNAME_FIELD,and REQUIRED_FIELD is some field that are not defined in AbstractBaseUser and PermissionMixin which my UserModel inherited, i can access all section except the 'Po User add section', and i put on my codes in models.py and admin.py and settings.py ,urls.py ,wish somebody could help. Thank you so much

error traces:

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'

Answer

Brian Dant picture Brian Dant · Jun 21, 2013

The parent class (django.contrib.auth.admin.UserAdmin) has an add_fieldsets attribute that includes the username field. Add an attribute to your MyUserAdmin class called add_fieldsets and treat it like the fieldsets attribute: use it to define fields you want to show in the add form.

Note: If your username is set to email then add email to add_fieldsets.

See the note about add_fieldset at the "Customizing authentication in Django" docs page and the full example from the Django docs.