How to make a ModelForm having a ForeignKey select with 1000's options lighter?

Hi,
my ModelForm has a ForeignKey field which creates a select-option dropdown with 1000’s of options. This form is very slow and not so user-friendly. I know that in the Admin side I can (and have) use the raw_id_fields = ["…"] approach, see https://books.agiliq.com/projects/django-admin-cookbook/en/latest/many_fks.html

I’m hoping to achieve similar functionality in my form. Do you have any best practices or good suggestion on how to do this? My Django/Python skills are quite limited, so a simple example would be nice to have.
-Kari

I ended up using the Django Select2 Widgets

I’d just override the query set to narrow it down - if you can do that - using get_queryset in the views or forms. If that isn’t appropriate, I’d use a pre-selection form where the number of Foreign Keys can be reduced or shown say 25/page with a number of different sorting options that can be applied (similar to the pop-up in your link, but maybe showing more details plus sorting on those fields).

–Jim

1 Like

Hi Jim,

thank you for your answer. I’m going to check that solution more in detail after I have learned Django/Python a bit more.
-Kari