I would like to filter one of my list_filters by a field in the table that the foreign key points to.
My models:
class Organisation(models.Model):
name = models.CharField()
COMPANY = 'COMPANY'
CHARITY = 'CHARITY'
ORG_CHOICES = (
(COMPANY, 'COMPANY'),
(CHARITY, 'CHARITY'),
)
type = models.CharField(choices = ORG_CHOICES)
class Issue(models.Model):
name = models.CharField
charity = models.ForeignKey(Organisation)
I would like to put in IssueAdmin:
list_filter = (charity)
And for that to supply a list of charities. Currently it just lists everything in the organisation model, both the charities and the companies. For example I get this list in the filter at the moment:
oxfam
yamaha
greenpeace
microsoft
When I want a filter that lists:
oxfam
greenpeace
I could fix this by splitting the organisation table into two tables (charity and company) but that feels wrong.
It seems like SimpleListFilter should work, but I've not had any luck so far. Basically I would like something the uses the following filter and returns a list of charities for the filter:
Organisation.objects.filter(type = 'CHARITY')
My (poor) attempt at a filter:
class CharityFilter(SimpleListFilter):
title = _('Charity')
parameter = _('charity__type')
def lookups(self, request, model_admin):
return Organisation.objects.filter(type = 'CHARITY')
def queryset(self, request, queryset):
if not self.value() is not None:
return queryset.filter(type = 'CHARITY')
else:
return queryset
Can any one help me?
Why don't you just filter on the type, ie:
class OrganisationAdmin(admin.ModelAdmin):
list_filter = ("type", )
# etc
It would allow you to have either all organisations, companies only or charities only.
Else if you realy want to treat "Charities" and "Companies" as distinct entities with distinct admins but still keep them in the same table, you can use proxy models, but I don't see the point here.
Edit : to filter issues
based on organization type it works just the same as with querying on relayed models (not a surprise since it's exactly what happens)
class IssueAdmin(admin.ModelAdmin):
list_filter =("charity__type", )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With