Django Неизвестные поля (имена), указанные для PoUser

Я получил эту ошибку, потому что я создаю пользовательскую модель пользователя, и когда я получил все настройки, которые я просматриваю в django/contrib/auth/models.py, я установил адрес электронной почты как USERNAME_FIELD, а REQUIRED_FIELD - это поле, которое не определено в AbstractBaseUser и PermissionMixin, которые унаследовал мой UserModel, я могу получить доступ ко всем разделам, кроме "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'

2 ответа

Решение

Родительский класс (django.contrib.auth.admin.UserAdmin) имеет add_fieldsets атрибут, который включает в себя username поле. Добавьте атрибут к вашему MyUserAdmin класс называется add_fieldsets и относиться к нему как к fieldsets Атрибут - определяет поля, которые вы хотите показать в форме добавления.

Смотрите источник здесь для примера.

Добавьте атрибут add_fieldsets в свой класс adminModel, как показано ниже

      from django.contrib.auth.admin import UserAdmin
    
class CustomUserAdmin(UserAdmin):
        ...
        # add fields those needs to be visible while adding the data in form.
        add_fieldsets = (
            (None, {'fields': ('first_name', 'last_name', 'email', 'password', 'role', 'country_code', 'country',
                               'is_active', 'verified')}),
        )
        ...

Более подробную информацию можно найти здесь. https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#custom-users-and-django-contrib-admin

Другие вопросы по тегам