简体   繁体   English

Django使用自定义过滤器排除在list_filter中

[英]Django Exclude in list_filter with custom filter

Im trying to do 2 filters so that they appear in the django admin filter sidebar, the first one is the following https://gist.github.com/739760c7de861f76657f in this one I would like to do filter if Gold exclude that which starts with J and Silver is already working exclude all that does not start with J 我正在尝试做2个过滤器,以便它们出现在django管理员过滤器侧栏中,第一个是以下https://gist.github.com/739760c7de861f76657f在这个我想做过滤器,如果Gold排除以J和Silver已经在工作,排除所有不以J开头的内容

The second filter is a bit more complicated, I have a field in my DB that contains a weigth of several people, I would like to be able to filter by ranks of weigth... for example weigths between 50 and 70 and so on... 第二个过滤器稍微复杂一点,我在数据库中有一个字段,其中包含几个人的体重,我希望能够按体重级别进行过滤,例如介于50到70之间的体重等等。 ..

How would I do this... Thank you so much as always! 我该怎么办...一如既往的谢谢!

Got it done... 完成了...

class GoldorSilverFilter(DateFieldFilterSpec):
    def __init__(self, f, request, params, model, model_admin):
        super(GoldorSilverFilter, self).__init__(f, request, params, model, model_admin)

        self.links = (
            (_('All'), {}),
            (_('Gold'), {'%s__regex' % self.field.name: '^[^J]',}),
            (_('Silver'), {'%s__istartswith' % self.field.name: 'J',}),
        )

    def title(self):
        return _("Metal")
        #return self.field.name

FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'goldorsilverfilter', False), GoldorSilverFilter))

I know the return should not be tied to one value but did not know what to do with it... about the second poing well I used the same class but used %s_ gte and %s _lte 我知道返回值不应与一个值绑定,但不知道该如何处理...关于第二个方面,我使用了相同的类,但使用了%s_ gte和%s _lte

Hope this helps someone out.. 希望这可以帮助某人..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM