Django error on admin search: Cannot resolve keyword 'username' into field

时间:2016-08-05 17:38:51

标签: python django python-3.x django-admin

I am having a problem in my admin in Django. I have a list of users, I can add, edit and delete them without any problem. However, when I use the search bar to find specific users, i get the error:

Cannot resolve keyword 'username' into field. Choices are: bookreader, date_joined, email, full_name, groups, id, is_active, is_staff, is_superuser, is_verified, last_login, logentry, password, profile, reading, user_permissions, usuário, virtualcurrency

I dont know why this is happening. Here is my User Model:

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email'), unique=True, blank=False)
    full_name = models.CharField(_('name'), max_length=30, blank=False)

    is_verified = models.BooleanField(_('verified'), default=False)
    is_staff = models.BooleanField(_('staff status'), default=False)
    is_active = models.BooleanField(_('active'), default=True)
    date_joined = models.DateTimeField(_('date joined'), default=now)

    objects = UserManager()

    USERNAME_FIELD = 'email'

    class Meta:
        verbose_name = _('user')

    def __str__(self):
        return self.email

    def get_short_name(self):
        return self.full_name.partition(' ')[0]

    def get_full_name(self):
        return self.full_name

    def send_verification_mail(self, request):
        if not self.pk:
            self.save()

        return send_template_mail(
            'Verificação de email',
            'users/user_verification_email.html',
            {
                'user': self,
                'verification_url': request.build_absolute_uri(
                    reverse('users:user_verify', kwargs={
                        'uidb64': urlsafe_base64_encode(force_bytes(self.pk)),
                        'token': default_token_generator.make_token(self)
                    })
                )
            },
            [self.email]
        )

And the UserAdmin in admin.py:

class UserAdmin(UserAdmin_):
    list_display = ('email', 'full_name', 'is_staff')
    ordering = ('email',)
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        (_('Personal info'), {'fields': ('full_name',)}),
        (_('Permissions'), {'fields': (
            'is_active', 'is_verified', 'is_staff',
            'is_superuser', 'groups', 'user_permissions'
        )}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password'),
        }),
    )
    form = UserChangeForm
    add_form = UserSignupForm


admin.site.register(User, UserAdmin)

Why I am getting this error?

1 个答案:

答案 0 :(得分:1)

UserAdmin类的search_fields选项包含usernamefirst_namelast_namesource code)。这与您的User模型不兼容,因为您的模型没有这些字段。

您应该为search_fields课程更改UserAdmin,以使其与您的用户模型兼容。例如:

class UserAdmin(UserAdmin_):
    search_fields = ('email', 'full_name')