Django 管理员按文本字段过滤列表

发布于 2024-12-01 17:00:20 字数 404 浏览 1 评论 0 原文

我需要根据文本字段过滤管理列表。我希望能够过滤查询集以查找 TextField 值为 Null 的所有对象。

我尝试了以下操作:

 def filter_for_field(self, request, queryset):

    queryset=queryset.exclude(field__isnull=True)
    return queryset

我将其作为一种方法添加到我的 AdminModel 中,然后添加了属性“actions=['filter_for_field']。

我还尝试在没有 return 语句的情况下执行此操作,没有骰子。该操作显示在管理中但它没有删除 TextField 的空值对象,

我做错了什么?

有更好的方法吗?

I need to filter an admin list based on a TextField. I want to be able to filter the queryset for all objects whose TextField value is Null.

I tried the following:

 def filter_for_field(self, request, queryset):

    queryset=queryset.exclude(field__isnull=True)
    return queryset

I added that as a method to my AdminModel and then added the property "actions=['filter_for_field'].

I also tried to do it without a return statement, no dice. The action is showing up in the admin but it is not removing objects with a null value for the TextField.

What am I doing wrong?

Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情 2024-12-08 17:00:20

您可以使用 自定义 FilterSpec 创建自定义管理过滤器的功能。它现在在 Django 的 SVN 版本中可用,并计划在 Django 1.4

from django.contrib.admin import SimpleListFilter

class IsNullFilter(SimpleListFilter):
   # Human-readable title which will be displayed in the
   # right admin sidebar just above the filter options.
   title = _('Custom filter')

   # Parameter for the filter that will be used in the URL query.
   parameter_name = 'custom_filter'

   def lookups(self, request, model_admin):
       """
       Returns a list of tuples. The first element in each
       tuple is the coded value for the option that will
       appear in the URL query. The second element is the
       human-readable name for the option that will appear
       in the right sidebar.
       """
       return (
           ('True', _('is Null')),
           ('False', _('is not Null')),
       )

   def queryset(self, request, queryset):
       """
       Returns the filtered queryset based on the value
       provided in the query string and retrievable via
       `self.value()`.
       """

       if self.value() == 'True':
           return queryset.filter(costomfield__isnull=True)
       if self.value() == 'True':
           return queryset.filter(costomfield__isnull=False)

然后您需要将其传递到 ModelAdmin.list_filter 中:

class CustomModelAdmin(admin.ModelAdmin):
    list_filter = (IsNullFilter,)

You can use the custom FilterSpec functionality to create custom admin filter. It is available in the SVN version of Django right now and planned for Django 1.4.

from django.contrib.admin import SimpleListFilter

class IsNullFilter(SimpleListFilter):
   # Human-readable title which will be displayed in the
   # right admin sidebar just above the filter options.
   title = _('Custom filter')

   # Parameter for the filter that will be used in the URL query.
   parameter_name = 'custom_filter'

   def lookups(self, request, model_admin):
       """
       Returns a list of tuples. The first element in each
       tuple is the coded value for the option that will
       appear in the URL query. The second element is the
       human-readable name for the option that will appear
       in the right sidebar.
       """
       return (
           ('True', _('is Null')),
           ('False', _('is not Null')),
       )

   def queryset(self, request, queryset):
       """
       Returns the filtered queryset based on the value
       provided in the query string and retrievable via
       `self.value()`.
       """

       if self.value() == 'True':
           return queryset.filter(costomfield__isnull=True)
       if self.value() == 'True':
           return queryset.filter(costomfield__isnull=False)

Then you need to pass it in ModelAdmin.list_filter:

class CustomModelAdmin(admin.ModelAdmin):
    list_filter = (IsNullFilter,)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文