Ajax foreign key filter in Django admin interface

We have a solution to the problem now! So first the solution and then the problem:


The problem:


Just tried django-ajax-filtered-fields and like it very much. I’m looking for a solution for this problem:

My model looks like this:

class Artist(models.Model):

class Song(models.Model):
    artist = models.ForeignKey(Artist)
    album = models.ForeignKey(Album, blank=True, null=True)
class Album(models.Model):
    artist = models.ForeignKey(Artist)

So the user has to define an an artist when adding a song. He can also define an album but doesn’t have to. The album belongs to an artist.

In the Django admin interface I’d like to limit the choices for albums based upon what he selects in the field “artist”. That should change dynamically with AJAX. I will have a closer look to django-ajax-filtered-fields but didn’t find a solution to that specific problem. Are there plans to include such functionality in the Django admin interface?

The ajax filter should work with more than two levels also. For instance if there is an additional class like

class RecordLabel(models.Model):
    artist = models.ForeignKey(Artist)

So if i select a record label the choices for artist should be filtered based upon the selected record label the artist is signed to.

Perhaps it’s another usecase for django-ajax-filtered-fields in general? Or do you know another solution to my specific requirements?

Discussion in the Django developers group: http://groups.google.com/group/django-developers/browse_thread/thread/daa3bcdfa247c2ed

Django-filter is a reusable Django application for allowing users to filter queryset dynamically. But it seems not to be suitable for the admin interface.

See also:




Just using raw_id_fields which provides a filter and a search field: http://www.thecssdiv.co.uk/2008/09/django-raw-id-fields-explained-basically/

Just with jQuery AJAX: http://harvesthq.github.io/chosen/

3 thoughts on “Ajax foreign key filter in Django admin interface”

  1. Many thanks for the link to the solution! You saved my day (and probably night too) 🙂

Leave a Reply

Your email address will not be published.

For spam filtering purposes, please copy the number 3467 to the field below: