2

Django's choice field, using a select widget, will render out an unlimited number of <option> elements. This, of course, takes a very long time and wastes tons of memory when there are hundreds of choices. When it comes to foreign keys, something akin to contrib.admin's raw_id_fields could be used, but in the case of hard-coded values (e.g., all subdivisions in the world to choose from, which come from pycountry), I'm not sure what to do, other than A) use AJAX to provide a list (still a lot of values to pipe out) or B) use a char field for the widget, allowing the form to validate bad input.

Are there any best practices that don't involve the aforementioned A or B options, or even one that's easy|clean|pluggable which does involve option A?

2
  • This has blown up my site on several different levels: first, browser starts crashing due to too many DOM elements, then, after even more FKs in the DB, server runs out of ram. Mar 21, 2013 at 1:05
  • @Yuji'Tomita'Tomita - exactly. I can still remember the 2 times that my company's +10 servers have come to a screeching halt due to somebody forgetting to add raw_id_fields to an admin with an FK to User (of which there are over 4 million records)... ugh
    – orokusaki
    Mar 21, 2013 at 1:11

1 Answer 1

1

I had the same problem once and, in my opinion, the best way of dealing with this is using an autocomplete field. This is like a text field but user gets options as he write in that text input.

This is way better than a simple text field because you won't have to validate if value is correct or not: you can only select a valid value.

I try some apps in order to have autocomplete and the best that fits my needs was this:

https://github.com/yourlabs/django-autocomplete-light

Hope it helps!

Not the answer you're looking for? Browse other questions tagged or ask your own question.