根据外键中的字段,在Django管理中过滤list_filter [英] Filtering list_filter in Django admin based on field in foreign key

查看:467
本文介绍了根据外键中的字段,在Django管理中过滤list_filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过外键指向的表中的一个字段过滤我的一个list_filters。

I would like to filter one of my list_filters by a field in the table that the foreign key points to.

我的模型:

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)

我想放入IssueAdmin:

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

当我想要一个列表:

oxfam
greenpeace

可以通过将组织表分成两个表(慈善机构和公司)来解决这个问题,但是感觉错误。

I could fix this by splitting the organisation table into two tables (charity and company) but that feels wrong.

似乎SimpleListFilter应该可以工作,但是我没有到目前为止运气好基本上我想要的东西使用以下过滤器,并返回过滤器的慈善机构列表:

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

可以帮助我吗?

推荐答案

我正确的说,你试图过滤问题实例而不是他们所属的组织的类型,但是由组织实例themselv es,所以你会在右边有一个可能很长的组织清单来过滤问题,但是你只希望CHARITies出现在这个列表中。

If I'm getting it right you're trying to filter Issue instances not by the type of the Organization they belong to, but by the Organization instances themselves, so you will have on the right a potentially very long list of Organizations to filter Issues on, but you want only CHARITies to appear in this list.

这可能不是成为最好的解决方案,如果有很多组织,但只有当他们很少...

This may not be the best solution if there are many Organizations, but only if they're few...

无论如何,根据Django文档: https://docs.djangoproject.com/en/dev/ref/contrib/admin/ 过滤器的lookups()方法必须返回一个元组列表(coded_value,displayed_value)。

Anyway, as per the Django docs: https://docs.djangoproject.com/en/dev/ref/contrib/admin/ the lookups() method of a Filter must return a list of tuple (coded_value,displayed_value).

以下未经测试的代码应该适用于您的情况:

The following untested code should work in your case:

class CharityFilter(SimpleListFilter):
    title = _('Charity')
    parameter_name = 'charity'
    def lookups(self, request, model_admin):
        queryset = model_admin.queryset(request).filter(organisation__type='CHARITY')
        return queryset.values_list('organisation__pk','organisation__name').order_by('organisation__name')

    def queryset(self, request, queryset):
        if self.value():
          return queryset.filter(organisation=self.value())

这篇关于根据外键中的字段,在Django管理中过滤list_filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆