views:

30

answers:

1

How do I create a filter in Django Admin to only display records where an integer value lies between two values? For example, if I have a model Person, which has an age attribute, and I only want to display Person records where age is between 45 and 65.

A: 

I you simply want a filtered version of the list view, that you access via a link (say in the list view), for example to view only the related items of a model, you do something like this:

def admin_view_receipts(self, object):
    url = urlresolvers.reverse('admin:invoice_%s_changelist'%'receipt')
    params = urllib.urlencode({'invoice__id__exact': object.id})
    return '<a href="%s?%s">Receipts</a>' % (url, params)
admin_view_receipts.allow_tags = True
admin_view_receipts.short_description = 'Receipts'

This will take you to a list view for 'Reciepts', but only those linked to the selected Invoice.

If you want a filter that displays in the sidebar, you could try this snippet or this

zsquare