Django Administration Dealing with foreign keys referencing large tables


By default, Django renders ForeignKey fields as a <select> input. This can cause pages to be load really slowly if you have thousands or tens of thousand entries in the referenced table. And even if you have only hundreds of entries, it is quite uncomfortable to look for a particular entry among all.

A very handy external module for this is django-autocomplete-light (DAL). This enables to use autocomplete fields instead of <select> fields.

django-autocomplete-light example

from dal import autocomplete

class CityAutocomp(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        qs = City.objects.all()
        if self.q:
            qs = qs.filter(name__istartswith=self.q)
        return qs

urlpatterns = [
    url(r'^city-autocomp/$', CityAutocomp.as_view(), name='city-autocomp'),

from dal import autocomplete

class PlaceForm(forms.ModelForm):
    city = forms.ModelChoiceField(

    class Meta:
        model = Place
        fields = ['__all__']

class PlaceAdmin(admin.ModelAdmin):
    form = PlaceForm