1

I have a field Foo with a ForeignKey that points to User. I need to create filter in the admin that only displays User that have at least one Foo. This would be easy with the development version of Django, but I am stuck with the 1.3.

I have seen here how to add a custom filter using the undocumented FilterSpec class. My problem is that it requires to modify the User model. I could inherit from User, but I already ave a setup where additional data is put into a Profile model wiith a one-to.one link to User.

Is there a less intrusive way to add a custom filter to the User model?

Andrea
  • 20,253
  • 23
  • 114
  • 183
  • 1
    Wait for Django 1.4 (or run on trunk, but that's generally not a good idea), which will have custom filterspecs built-in. Otherwise, you're out of luck. – Chris Pratt Nov 14 '11 at 15:10
  • We have been running django from trunk since they got i18N_urls and it's fine. – Bite code Dec 27 '11 at 17:27

1 Answers1

1

You can actually use the foreign key relation backwards in an ORM query.

User.objects.filter(foo__isnull=False)
pyrospade
  • 7,870
  • 4
  • 36
  • 52
  • 1
    Just realized this is REALLY old. Hope this helps someone regardless! – pyrospade Jan 15 '13 at 04:12
  • Thank you - it is quite old indeed. :-) Anyway, the problem was not to filter those users manually, but instead to have this available as a filter in the Django admin. – Andrea Jan 15 '13 at 08:42
  • 1
    This seems in the ballpark - http://stackoverflow.com/questions/991926/custom-filter-in-django-admin-on-django-1-3-or-below – pyrospade Jan 15 '13 at 14:54