The question is similar to Django "list_filter" based on the admin queryset. I need to use list_filter based on current user. But in my case the field span relations using the __ lookup. There is no useful "lookups" method on RelatedFieldListFilter. My approach resulted in the next code:
def custom_titled_filter(title):
class Wrapper(admin.FieldListFilter):
def __new__(cls, field, request, *args, **kwargs):
instance = admin.FieldListFilter.create(field, request, *args, **kwargs)
instance.title = title
rel_model = field.remote_field.model
qs = rel_model._default_manager.all()
if not request.user.is_superuser:
qs = qs.filter(user=request.user)
choice_func = operator.attrgetter(
field.remote_field.get_related_field().attname
if hasattr(field.remote_field, 'get_related_field')
else 'pk'
)
instance.lookup_choices = [
(choice_func(x), str(x)) for x in qs
]
return instance
return Wrapper
Is there any better approach with current version of Django?