在 Django Admin 中过滤过滤器

发布于 2024-10-18 03:15:33 字数 432 浏览 0 评论 0原文

这是我的学校项目代码 http://dpaste.com/434311/

代码工作正常,在学生管理列表页面上,我得到了过滤器类 这很好,但正如你所看到的,我的项目是多租户的,所以在过滤器中 我只想显示当前用户所在学校的课程的区域 已登录(通过会话进行跟踪),但现在我可以看到所有列表 所有学校的课程

,所以我想

list_filter   = ['xclass']  

用诸如“

list_filter   = Class.objects.filter(school=request.session['school_id'])

我该怎么做?”之类的内容替换这一行?

Here is my code for a school project
http://dpaste.com/434311/

The code works fine, on studentadmin list page, I get filter for classes
which is good but as you can see my project is multi-tenant so in filter
area I want to show only the classes for the school the current user is
logged in (tracked thru sessions) but right now I can see list of all
classes from all schools

so I want to replace this line

list_filter   = ['xclass']  

with something like

list_filter   = Class.objects.filter(school=request.session['school_id'])

how can I do it ?

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

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

发布评论

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

评论(2

够运 2024-10-25 03:15:33

从 2012 年 3 月 23 日发布的 1.4 版本开始,您可以使用官方的 django.contrib.admin.SimpleListFilter

以下是仅用于列出活跃公司的过滤器代码示例:

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

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

    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.
        """
        lookup_list = Company.objects.active().values_list('id', 'name').distinct()
        # 'objects.active' is a custom Company manager and is 
        # equivalent to filter 'objects.filter(status=100)'  
        return lookup_list

    def queryset(self, request, queryset):
        """
        Returns the filtered queryset based on the value
        provided in the query string and retrievable via
        `self.value()`.
        """
        # Compare the requested value (either '80s' or 'other')
        # to decide how to filter the queryset.
        if self.value():
            return queryset.filter(project__company=self.value())

As of version 1.4 released March 23, 2012 you can use the official django.contrib.admin.SimpleListFilter

Here is an example of filter code used to that list active company only:

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

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

    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.
        """
        lookup_list = Company.objects.active().values_list('id', 'name').distinct()
        # 'objects.active' is a custom Company manager and is 
        # equivalent to filter 'objects.filter(status=100)'  
        return lookup_list

    def queryset(self, request, queryset):
        """
        Returns the filtered queryset based on the value
        provided in the query string and retrievable via
        `self.value()`.
        """
        # Compare the requested value (either '80s' or 'other')
        # to decide how to filter the queryset.
        if self.value():
            return queryset.filter(project__company=self.value())
梦巷 2024-10-25 03:15:33

我多次遇到过这个问题。不幸的是,您将需要编写一个自定义的、未记录的FilterSpec

Django 1.3 或更低版本上的 Django Admin 中的自定义过滤器

它正在开发中,所以应该在这里很快...
http://code.djangoproject.com/ticket/5833


另一种方法是修改基础queryset 用于仅包含您学校 ID 的列表页面。
http://docs.djangoproject .com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.queryset

def queryset(self, request):
    qs = super(MyModelAdmin, self).queryset(request)
    return qs.filter(xclass__school__id=request.session['school_id'])

I've run into this issue many times. Unfortunately, you are going to need to write a custom, undocumented FilterSpec.

Custom Filter in Django Admin on Django 1.3 or below

It's being worked on so should be here soon...
http://code.djangoproject.com/ticket/5833


An alternative is to modify the base queryset for the list page with only those from your school id.
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.queryset

def queryset(self, request):
    qs = super(MyModelAdmin, self).queryset(request)
    return qs.filter(xclass__school__id=request.session['school_id'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文