Django Admin:如何过滤特定值范围的整数字段

时间:2022-08-25 10:27:23

How do I create a filter in Django Admin to only display records where an integer value lies between two values? For example, if I have a model Person, which has an age attribute, and I only want to display Person records where age is between 45 and 65.

如何在Django Admin中创建一个过滤器,只显示整数值介于两个值之间的记录?例如,如果我有一个具有年龄属性的模型Person,并且我只想显示年龄在45到65之间的Person记录。

4 个解决方案

#1


1  

What you are looking is http://djangosnippets.org/snippets/587/ - the snippet is kinda old but works just fine after an additional minor change.

您正在寻找的是http://djangosnippets.org/snippets/587/ - 该片段有点陈旧但经过一次额外的微小改动后效果很好。

I uploaded the patched version at https://gist.github.com/1009903

我在https://gist.github.com/1009903上传了修补后的版本

#2


1  

You can Filter the field some what like this by using queryset() Function ... I had used SimpleListFilter

您可以使用queryset()函数过滤字段,使用SimpleListFilter

    def queryset(self, request, queryset):
        filt_age = request.GET.get('parameter_name')
        return queryset.filter(
                    age__range=self.age_dict[filt_age]
                )

And create dict in lookups() and return it According to the age

并在lookups()中创建dict并根据年龄返回它

    def lookups(self, request, model_admin):
    return [
        (1, '5-21'),
        (2, '22-35'),
        (3, '35-60')
    ]

#3


0  

I you simply want a filtered version of the list view, that you access via a link (say in the list view), for example to view only the related items of a model, you do something like this:

我只想要一个列表视图的过滤版本,您可以通过链接(例如在列表视图中)访问,例如,仅查看模型的相关项,您执行以下操作:

def admin_view_receipts(self, object):
    url = urlresolvers.reverse('admin:invoice_%s_changelist'%'receipt')
    params = urllib.urlencode({'invoice__id__exact': object.id})
    return '<a href="%s?%s">Receipts</a>' % (url, params)
admin_view_receipts.allow_tags = True
admin_view_receipts.short_description = 'Receipts'

This will take you to a list view for 'Reciepts', but only those linked to the selected Invoice.

这将带您进入“Reciepts”的列表视图,但只会链接到所选的Invoice。

If you want a filter that displays in the sidebar, you could try this snippet or this

如果您希望在侧边栏中显示过滤器,则可以尝试此片段或此片段

#4


0  

Based on another answer for a related question, I learnt that there is an officially documented way to do that since version 1.4. It even includes an example of filtering by date.

基于对相关问题的另一个答案,我了解到自1.4版以来有一种正式记录的方法。它甚至包括按日期过滤的示例。

Still, the snippet in the sorin answer is also interesting, because it just adds django-style parameters to the URL, which is a different solution than the official documentation example.

尽管如此,sorin答案中的片段也很有趣,因为它只是将django样式的参数添加到URL,这是一个与官方文档示例不同的解决方案。

#1


1  

What you are looking is http://djangosnippets.org/snippets/587/ - the snippet is kinda old but works just fine after an additional minor change.

您正在寻找的是http://djangosnippets.org/snippets/587/ - 该片段有点陈旧但经过一次额外的微小改动后效果很好。

I uploaded the patched version at https://gist.github.com/1009903

我在https://gist.github.com/1009903上传了修补后的版本

#2


1  

You can Filter the field some what like this by using queryset() Function ... I had used SimpleListFilter

您可以使用queryset()函数过滤字段,使用SimpleListFilter

    def queryset(self, request, queryset):
        filt_age = request.GET.get('parameter_name')
        return queryset.filter(
                    age__range=self.age_dict[filt_age]
                )

And create dict in lookups() and return it According to the age

并在lookups()中创建dict并根据年龄返回它

    def lookups(self, request, model_admin):
    return [
        (1, '5-21'),
        (2, '22-35'),
        (3, '35-60')
    ]

#3


0  

I you simply want a filtered version of the list view, that you access via a link (say in the list view), for example to view only the related items of a model, you do something like this:

我只想要一个列表视图的过滤版本,您可以通过链接(例如在列表视图中)访问,例如,仅查看模型的相关项,您执行以下操作:

def admin_view_receipts(self, object):
    url = urlresolvers.reverse('admin:invoice_%s_changelist'%'receipt')
    params = urllib.urlencode({'invoice__id__exact': object.id})
    return '<a href="%s?%s">Receipts</a>' % (url, params)
admin_view_receipts.allow_tags = True
admin_view_receipts.short_description = 'Receipts'

This will take you to a list view for 'Reciepts', but only those linked to the selected Invoice.

这将带您进入“Reciepts”的列表视图,但只会链接到所选的Invoice。

If you want a filter that displays in the sidebar, you could try this snippet or this

如果您希望在侧边栏中显示过滤器,则可以尝试此片段或此片段

#4


0  

Based on another answer for a related question, I learnt that there is an officially documented way to do that since version 1.4. It even includes an example of filtering by date.

基于对相关问题的另一个答案,我了解到自1.4版以来有一种正式记录的方法。它甚至包括按日期过滤的示例。

Still, the snippet in the sorin answer is also interesting, because it just adds django-style parameters to the URL, which is a different solution than the official documentation example.

尽管如此,sorin答案中的片段也很有趣,因为它只是将django样式的参数添加到URL,这是一个与官方文档示例不同的解决方案。