Ajax foreign key filter in Django admin interface

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

http://github.com/digi604/django-smart-selects

The problem:

http://code.google.com/p/django-ajax-filtered-fields/

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


http://github.com/alex/django-filter
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:

http://stackoverflow.com/questions/12170267/django-filter-many-to-many-field-in-admin

http://django-selectable.readthedocs.org/en/version-0.5.2/advanced.html#chained-selection

http://django-autocomplete-light.readthedocs.org/en/latest/dependant.html

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”

Leave a Reply

Your email address will not be published. Required fields are marked *

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