Technology for ponies

Dec 17, 2013

Admin interface with foreign key on a very long list

If your application has a model with a ForeignKey on an other model which gets a lot of entries in the database, the auto-generated admin interface can become a nightmare to load. Django will render your ForeignKey using a select drop-down and with thousands of entries loading time and browser memory are in bad shape.

Solution is rather simple, add your ForeignKey in the raw_id_fields parameter in the Django admin. Lets consider this example :


class MyModel(models.Model):
  user = models.ForeignKey(User)


from models.py import MyModel

class MyModelAdmin(admin.ModelAdmin):
    raw_id_fields = ("user",)

admin.site.register(MyModel, MyModelAdmin)