Django 管理,通过 ManyToMany 引用过滤对象
有 photologue 应用程序,django 的简单照片库,实现照片和图库对象。 Gallery 对象有 ManyToMany 字段,该字段引用 Photo 对象。
我需要能够获取给定图库的所有照片列表。是否可以将图库过滤器添加到照片的管理页面? 如果可以的话,怎样做最好?
There's photologue application, simple photo gallery for django, implementing Photo and Gallery objects.
Gallery object has ManyToMany field, which references Photo objects.
I need to be able to get list of all Photos for a given Gallery. Is it possible to add Gallery filter to Photo's admin page?
If it's possible, how to do it best?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要编写一个自定义 FilterSpec! Django 1.3 或更低版本的 Django Admin 中的自定义过滤器
它将如下所示:
将其放入应用程序包中的模块
filters.py
中,并将其导入到admin.py
中(导入它很重要,因此过滤器将在管理站点上注册!)编辑:“f”是字段实例,在本例中是
models.ManyToManyField
最后一行为与 Gallery 模型相关的所有字段注册 FilterSpec 。如果该字段是在 Gallery 模型上定义的,则这将不起作用,因为 django.contrib.admin.views.main.ChangeList.get_filters 会检查您在列表中定义的字段是否确实存在于模型(也不适用于 related_name)。我认为最简单的方法是,您可以为该更改列表创建一个自定义模板,并在其中硬编码您的过滤器,FilterSpec 本身不需要过滤本身,django 仅使用 url get 参数!You need to write a custom FilterSpec! Custom Filter in Django Admin on Django 1.3 or below
It'll look like this:
Put it in a module
filters.py
in your app package and import it in youadmin.py
(it's important to import it, so that the filter becomes registered on the admin site!)EDIT: "f" is the field instance, in this case
models.ManyToManyField
The last line registers the FilterSpec for all fields that have a relation to the Gallery model. This will not work as you mentioned if the field is defined on the Gallery model, sincedjango.contrib.admin.views.main.ChangeList.get_filters
checks if the field you define in the list really exist on the model (doesnt work for related_name either). I think the easiest way around is that you could make a custom template for that changelist and hardcode your filter in there, the FilterSpec itself isn't need for the filtering itself, django uses just the url get parameters for that!嗯,我就是这么做的。
我制作了自定义管理模板“change_list.html”。自定义模板标签创建所有现有画廊的列表。过滤是这样进行的:
Cookie是用javascript设置的。
Well, that's how I've done it.
I made custom admin template "change_list.html". Custom template tag creates a list of all existing galleries. Filtering is made like this:
Cookie is set with javascript.
为了供其他人将来查找参考,如果您有关系,它是双向的,因此您可以通过 ModelAdmin 获取图库的照片或照片的图库。
假设您的照片模型有一个更改列表视图:
然后在管理中您将看到一个显示所有图库的过滤器,如果您单击其中一个,它将过滤列表以仅向您显示该图库的照片。
当然,如果有很多画廊,这可能不切实际,但您只需使用记录良好的 ModelAdmin 即可到达那里,而不是拼凑模板或过滤器规范。
http://docs.djangoproject.com/en/dev/参考/contrib/admin/#modeladmin-objects
For future reference for others to find, if you have a relationship it's bi-directional, so you can get the photos for galleries or the galleries for a photo via a ModelAdmin.
Let's say you have a changelist view for your Photo model:
Then in the admin you'll see a filter showing all of the galleries and if you click one it'll filter the list to show you only photos for that gallery.
Of course, this may not be practical if there are a LOT of galleries, but you can get there just by using the well-documented ModelAdmin rather than hacking together a template or filterspec.
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-objects
@Jough Dempsey 指出您可能不需要仅用于 m2m 字段的自定义 FilterSpec。
然而今天我发现我想要一个 django-taggit 标签字段。标签基本上是一种 m2m 关系,但如果您尝试将标签字段添加到 list_filter 中,它会抱怨
'TaggableManager' 对象没有属性 'get_choices'
。在这种情况下,@lazerscience 的代码可以拯救...
但是,当用于 Django 1.3 时,它不起作用,需要添加几行新行,比较我的以下版本:
@Jough Dempsey pointed out you maybe don't need a custom FilterSpec just for m2m fields.
However today I found I wanted one for a django-taggit tag field. The tags are basically an m2m relation but it complains that
'TaggableManager' object has no attribute 'get_choices'
if you try and add the tag field into list_filter.In this case it was @lazerscience's code to the rescue...
However it didn't work when used against Django 1.3, needed a couple of new lines added, compare my version below which works: