负自定义 Django admin FilterSpec

发布于 2024-08-10 19:00:01 字数 865 浏览 3 评论 0原文

我正在开发自定义 Django Admin FilterSpec (已在 SO #991926 上介绍) 。 My FilterSpec 是ForeignKey(User) 上默认过滤器的替代品,基本上用三个唯一的选择(全部、我的和其他)替代所有用户的列表。

例如,如果我将自定义过滤器规范应用到字段created_by,它将添加一个包含“全部”、“由我创建”和“由其他人创建”的管理过滤器。一切正常除了负面过滤器,由其他人创建。

我一直试图通过将 __not 附加到查询来实现此目的:

def choices(self, cl):
    yield {
            'selected': self.lookup_val == self.user.pk,
            'query_string': cl.get_query_string({'%s__not' % self.field.name: self.user.pk}),
            'display': capfirst('%s Others' % self.field.verbose_name)
        }

Django 似乎不支持像这样的否定过滤。我还尝试过让它执行 __gte__lte 但 filterspec 仅使用它找到的第一个 (gte),而放弃另一个 (lte)。

有人知道如何通过自定义 FilterSpec 实现这样的负过滤器吗?

I'm working on a custom Django Admin FilterSpec (covered already on SO #991926). My FilterSpec is a replacement for the default filter on ForeignKey(User), and basically replaces the list of all users with three only choices, all, mine, and others.

For example, if I applied the custom filterspec to the field created_by it would add an admin filter with All, Created by Me, and Created by Others. Everything works except the negative filter, Created by Others.

I've been attempting to achieve this by appending __not to the query as so:

def choices(self, cl):
    yield {
            'selected': self.lookup_val == self.user.pk,
            'query_string': cl.get_query_string({'%s__not' % self.field.name: self.user.pk}),
            'display': capfirst('%s Others' % self.field.verbose_name)
        }

It doesn't seem that Django supports filtering in the negative like this. I've also experimented with having it do a __gte and __lte but the filterspec only uses the first one it finds (gte), dropping the other (lte).

Anybody know how to achieve a negative filter like this through a custom FilterSpec?

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

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

发布评论

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

评论(2

梦与时光遇 2024-08-17 19:00:01

此功能还不是 Django 代码的一部分;计划发布 1.2 版本。您需要将此补丁应用于 Django 代码: http://code.djangoproject.com/ticket /5833

使用FilterSpecget_query_set()方法。例如:

class AlunoStatus(FilterSpec):
    def __init__(self, request, params, model, model_admin):
        self.lookup_val = request.GET.get('exclude_value', None)

    def get_query_set(self, cl, qs):
        if self.lookup_val:
            qs = qs.exclude(field=self.lookup_val)
        return qs

    def choices(self, cl):
        yield {'selected': self.lookup_val is None,
               'query_string': cl.get_query_string({}, ['exclude_value']),
               'display': _('All')}
        for choice in choices:
            yield {'selected': self.lookup_val == choice,
                    'query_string': cl.get_query_string({'exclude_value': choice}),
                    'display': u"Exclude "+smart_unicode(choice)}

我没有对此进行测试,但我希望您能明白。

This feature is not part of the Django code yet; it is planned for version 1.2. You'll need to apply this patch to the Django code: http://code.djangoproject.com/ticket/5833

Use the get_query_set() method of FilterSpec. For example:

class AlunoStatus(FilterSpec):
    def __init__(self, request, params, model, model_admin):
        self.lookup_val = request.GET.get('exclude_value', None)

    def get_query_set(self, cl, qs):
        if self.lookup_val:
            qs = qs.exclude(field=self.lookup_val)
        return qs

    def choices(self, cl):
        yield {'selected': self.lookup_val is None,
               'query_string': cl.get_query_string({}, ['exclude_value']),
               'display': _('All')}
        for choice in choices:
            yield {'selected': self.lookup_val == choice,
                    'query_string': cl.get_query_string({'exclude_value': choice}),
                    'display': u"Exclude "+smart_unicode(choice)}

I didn't test this, but I hope you get the idea.

糖果控 2024-08-17 19:00:01

使用“排除”过滤器进行否定不会有效吗?

Won't using a "exclude" filter for the negation work?

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