在 Django Admin 中过滤过滤器
这是我的学校项目代码 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 2012 年 3 月 23 日发布的 1.4 版本开始,您可以使用官方的 django.contrib.admin.SimpleListFilter
以下是仅用于列出活跃公司的过滤器代码示例:
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:
我多次遇到过这个问题。不幸的是,您将需要编写一个自定义的、未记录的
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
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