Tales Pádua
Tales Pádua

Reputation: 1461

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

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?

Upvotes: 1

Views: 1789

Answers (1)

Alasdair
Alasdair

Reputation: 309049

The search_fields option for the UserAdmin class contains username, first_name and last_name (source code). This is not compatible with your User model, because your model does not have these fields.

You should change search_fields for your UserAdmin class, so that it is compatible with your user model. For example:

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

Upvotes: 3

Related Questions