django-filer
django-filer copied to clipboard
Admin search fails with custom User model as owner
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.
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.
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.
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.
This will now be closed due to inactivity, but feel free to reopen it.