I have a Django model containing a text field. In the admin GUI, I’d like to be able to filter just those records containing text in this field. Is it possible?
Code something like this will filter on the contents of textfield, but shows filters on ‘All’ and each distinct entry in the filter. I’d like to filter ‘All’ or ‘Contains something’.
class MyModel(models.Model): # ... textfield = models.CharField(max_length=100) # ... class MyModelAdmin(admin.ModelAdmin): list_display = ('...', 'textfield', '...') list_filter = ('...', 'textfield', '...')
Yes, but it’s not documented. Look at
django\contrib\admin\filterspecs.pyto see how the default filterspecs are created and how you can create your own. This feature is planned for version 1.1, and there’s already a patch if you want to try the feature now on the latest HEAD revision: http://code.djangoproject.com/ticket/5833Also not documented is the fact that you can put arbitrary GET parameters on the URL of your change_list page to filter the results. For example
/admin/app/model/?field1__lte=5&field2__gte=10Unfortunately?field__isnull=Truewon’t work, but you can easily experiment to see if you could find a filter that would work in your case.