django-filer icon indicating copy to clipboard operation
django-filer copied to clipboard

Admin search fails with custom User model as owner

Open tangram opened this issue 11 years ago • 3 comments

My custom User model does not have username, first_name or last_name fields. Admin search field fails with FieldError: Cannot resolve keyword 'username' into field.

See filter_folder and filter_file methods.

tangram avatar May 27 '14 10:05 tangram

It's not ideal, but here's what we do to get around this bug:

from django.contrib import admin

# Fix filer's search
from django.db.models import Q

from filer.admin import FolderAdmin
from filer.models import Folder


class CustomUserModelFilerAdmin(FolderAdmin):
    @staticmethod
    def filter_folder(qs, terms=[]):
        for term in terms:
            qs = qs.filter(
                Q(name__icontains=term) |
                Q(owner__email__icontains=term) |
                Q(owner__name__icontains=term)
            )
        return qs

    @staticmethod
    def filter_file(qs, terms=[]):
        for term in terms:
            qs = qs.filter(
                Q(name__icontains=term) |
                Q(description__icontains=term) |
                Q(original_filename__icontains=term) |
                Q(owner__email__icontains=term) |
                Q(owner__name__icontains=term)
            )
        return qs


admin.site.unregister(Folder)
admin.site.register(Folder, CustomUserModelFilerAdmin)

We just stick it in an admin.py file. This code is specific to our custom user model though, so you'll have to edit it. It would be nice to have a generic version that just worked in filer.

rockymeza avatar Jul 08 '14 08:07 rockymeza

Thanks for this approach, @rockymeza. I hadn't thought about overriding this way.

I worked on a generic solution for filer by checking the registered user model for present fields and building the query that way, but it got ugly rather quick. Maybe I can give it another shot.

tangram avatar Jul 08 '14 09:07 tangram

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 29 '22 00:07 stale[bot]

This will now be closed due to inactivity, but feel free to reopen it.

stale[bot] avatar Aug 30 '22 17:08 stale[bot]