Django: When a foreign key links to a huge list of data

Imagine the following model for your Django application:

class Address(models.Model):
    ...

class Person(models.Model):
    address = models.ForeignKey(Address)
    ...

The Django admin inteface for Person will show a DropDownList with many many items. For the user this is hardly usable since it will be quite cumbersome to pick the correct entry from that list when you want to change a person’s address. If you even want to edit the person’s address you have to change to the Address model admin view, search the item and edit it there. Not nice.

The solution is hidden very nicely in the Django documentation about raw_id_fields. Just add the following code to your model admin:

class ContactPersonAdmin(model.ModelAdmin):
    model = ContactPerson
    raw_id_fields = ("address",)

This will result in the following:

Now you can click the icon and search for the address you need.

  • Share/Bookmark

Tags:

One Response to “Django: When a foreign key links to a huge list of data”

  1. Bingimar Says:
    April 20th, 2010 at 7:16 pm

    awesome thanks!

Leave a Reply