In the Admin, Is it possible to make filters on foreign keys usable with lots of related objects?

201 views
Skip to first unread message

Paul Tiplady

unread,
Feb 2, 2018, 1:23:50 PM2/2/18
to Django users
Currently it's simple to configure a filter on a foreign key in the admin:

`list_filter = ['theforeignkeyfield']`

However in practice this is barely usable in most cases that I've encountered, since the admin uses RelatedFieldListFilter, which fetches the full list of objects from the DB and loads them into the browser. For any production site that either times out or makes the admin unusably slow, not to mention the UX is terrible as it inserts a list of all of the items into the filter bar that needs to be scrolled through.

I'm not sure if this is fixed in 2.0 (I haven't played with the new select2 integration that was added), but is there a plugin / workaround to use something like django-autocomplete-light, or a `raw_id_field` type approach in pre-2.0?

Andy

unread,
Feb 2, 2018, 3:57:46 PM2/2/18
to Django users
You should rather use search_fields instead of filtering together with the sorting ability of the admin. Thats much more suited for a scenario like described above.
The filter is for status fields or something with just a few different entries, otherwise build your own filter to filter for some specific values.

Paul Tiplady

unread,
Feb 2, 2018, 4:55:41 PM2/2/18
to django...@googlegroups.com
That's a pretty inflexible solution though; what if I want to filter on multiple foreign keys, e.g. Tenant=Foo(id=1), Customer=Bar(id=200)?

On Fri, Feb 2, 2018 at 12:57 PM, Andy <kaku...@gmail.com> wrote:
You should rather use search_fields instead of filtering together with the sorting ability of the admin. Thats much more suited for a scenario like described above.
The filter is for status fields or something with just a few different entries, otherwise build your own filter to filter for some specific values.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/j2pDXmEGvJE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1ffa9984-7775-49d1-bc78-6dd2de7fcf5d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Mike Dewhirst

unread,
Feb 2, 2018, 6:58:53 PM2/2/18
to django-users@googlegroups.com >> Django users
Have you looked at  ...

https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey

M


> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/4e610074-ff8c-494d-977e-cf701c8cd92f%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/4e610074-ff8c-494d-977e-cf701c8cd92f%40googlegroups.com?utm_medium=email&utm_source=footer>.

Paul Tiplady

unread,
Feb 2, 2018, 7:10:18 PM2/2/18
to django...@googlegroups.com
I've used that in a few admin pages, as far a I understand it just restricts the allowed values for a model's foreignkey field when editing a model instance. I don't see any way to use that to change how the `list_filter` functions -- am I missing something?

Thanks,
Paul

On Fri, Feb 2, 2018 at 3:57 PM, Mike Dewhirst <mi...@dewhirst.com.au> wrote:
On 3/02/2018 5:23 AM, Paul Tiplady wrote:
Currently it's simple to configure a filter on a foreign key in the admin:

`list_filter = ['theforeignkeyfield']`

However in practice this is barely usable in most cases that I've encountered, since the admin uses RelatedFieldListFilter, which fetches the full list of objects from the DB and loads them into the browser. For any production site that either times out or makes the admin unusably slow, not to mention the UX is terrible as it inserts a list of all of the items into the filter bar that needs to be scrolled through.

I'm not sure if this is fixed in 2.0 (I haven't played with the new select2 integration that was added), but is there a plugin / workaround to use something like django-autocomplete-light, or a `raw_id_field` type approach in pre-2.0?

Have you looked at  ...

https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey

M


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com <mailto:django-users+unsubscrib...@googlegroups.com>.
To post to this group, send email to django...@googlegroups.com <mailto:django-users@googlegroups.com>.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/j2pDXmEGvJE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Andy

unread,
Feb 2, 2018, 11:36:49 PM2/2/18
to Django users
No you didn’t miss anything. He did not understand your question.
But like I said before you can always build your own filter. Your problem is highly custom to your DB and model relations. You can’t expect any framework to already have a matching component for that.

In my 9 years of Django development I never needed what you expect to be there.

Matemática A3K

unread,
Feb 5, 2018, 9:40:18 AM2/5/18
to django...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages