有没有办法过滤django管理员中的查询集? [英] Is there a way to filter a queryset in the django admin?

查看:129
本文介绍了有没有办法过滤django管理员中的查询集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为模型栏定义一个动作 - 但是我只想要与用户Foo相关的Bar对象列表。

I'm trying to define an action for a model Bar -- but I only want the list of Bar objects related to a user Foo.

在我开始之前在管理代码中丢弃并传递自定义查询器(或编写劫持管理员多重复选框支持的自定义视图),我想我会检查是否有一种方法可以分解数据,因为我得到一个列表视图。

Before I start mucking around in the admin code and passing in a custom queryset (or writing a custom view that hijacks the admin's multi-checkbox support), I figured I'd check to see if there were a way I could slice up the data as is so I get a list view.

注意:我没有尝试按相关用户排序,我不会尝试添加额外的选项到默认列表视图按用户过滤;我需要一个特定的URL或视图,它将给我 一个Bar对象的列表 用户Foo。

Note: I'm not trying to sort by related user, and I'm not trying to add extra options to the default list view to filter by user; I need a specific URL or view that will give me just a list of Bar objects to a specific user Foo.

推荐答案

没有记录,但是标准的更改列表视图接受正常的查询过滤器参数作为GET参数。所以你可以做:

It's not documented, but the standard changelist view accepts normal queryset filter parameters as GET arguments. So you can do:

/admin/myapp/bar/?user__username=foo

这篇关于有没有办法过滤django管理员中的查询集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆