[Answered ]-Master/detail using the admin in django

2👍

Prior to Django 1.2.4, you could create filtered links to models in the admin using GET query like this: http://your_site.com/admin/your_app/detail/?master__id__exact=2.

But it was a bit of security hole, and got fixed. Now you’ll get a SuspiciousOperation exception if you try to filter your models using lookup that is not specified in list_filter attribute.

Though there’s a fix for that. This workaround implements a valid_lookups attribute so that you can perform filtering using some lookups via URL get query, without having these lookups exposed in the admin interface.

For this to work, you’ll need to override lookup_allowed() method on your ModelAdmin. Here is the example code, check the post mentioned above for details.

class DetailAdmin(admin.ModelAdmin):
    valid_lookups = ()
    def lookup_allowed(self, lookup, *args, **kwargs):
        if lookup.startswith(self.valid_lookups):
            return True
         return super(DetailAdmin, self).lookup_allowed(lookup, *args, **kwargs)

Leave a comment