更好的 Django 管理 ManyToMany 字段小部件
我发现 Django Admin 的默认 models.ManyToManyField
小部件使用起来很麻烦。这是 HTML select 元素,如果您有很多“其他”模型的对象那么实际找到想要与“这个”对象关联的“其他”对象是相当不切实际的。如果您有很多“其他”模型的对象,它似乎甚至会减慢管理页面的渲染速度。
我知道我可以构建自己的自定义管理小部件,并将其应用到我认为合适的 ManyToManyFields
中,但是是否有任何我可以使用的预构建小部件?在我的梦中,我想象了一个自动完成文本输入 HTML 小部件。这在 Django 管理框架中可行/可能吗?
谢谢。
I find the the Django Admin's default models.ManyToManyField
widget to be cumbersome to use. It's the HTML select element and if you have a lot of Objects of the "other" model then it's quite impractical to actually find the "other" Objects you want to associate with "this" Object. And if you have a lot of objects of the "other" model it seems to even slows down the rendering of the Admin page.
I'm aware that I can build my own custom admin widget and apply it to my ManyToManyFields
as I see fit, but are there any pre-built ones out there that I might use instead? In my dreams, I picture an auto-completing text input HTML widget. Is this even practical/possible to do in the Django admin framework?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试在管理类上使用
filter_horizontal
属性,例如:如 文档,“将 ManyToManyField 添加到此列表将改为使用一个漂亮的、不显眼的 JavaScript“过滤器”界面,该界面允许在选项中进行搜索”。
filter_vertical
确实相同的东西,布局略有不同。Try using the
filter_horizontal
attribute on your admin class, for example:As mentioned in the documentation, "adding a ManyToManyField to this list will instead use a nifty unobtrusive JavaScript "filter" interface that allows searching within the options".
filter_vertical
does the same thing with a slightly different layout.您可以尝试在管理中使用原始 ID。
和 Django 文档:
http://docs.djangoproject.com/en/dev /ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields
如果您正在寻找具有自动完成功能的内容,您可能需要将此视为起点 http://code.djangoproject.com/wiki/AutoCompleteSolutions
最后是一个非常简单的内联示例:
models.py
admin.py:
you could try using a raw id in the admin.
and the django docs:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields
if you are looking for something with auto-complete you might want to look at this as a starting point http://code.djangoproject.com/wiki/AutoCompleteSolutions
and finally a very simplistic inline Example:
models.py
admin.py:
我实际上并没有使用它,但我发现这个看起来很有前途的库在其他地方引用。
它似乎完全符合我的要求。它不像
filter_horizontal
那样加载相关对象的整个列表(无论有多少!)并为您提供一个选择器来选择其中的一些对象,而是提供一个搜索/过滤框,使用预先输入/自动完成调用来动态检索结果。如果您可能有 5000 个用户,并且想要选择其中的 3 或 4 个用户,而无需等待 5k元素下载和渲染,那么这非常有用。
I haven't actually played with it but I found this promising looking library referenced elsewhere.
It appears to do exactly what I wanted. Rather than loading the entire list of related objects (regardless of how many there are!) and presenting you with a picker to select a few of them, as
filter_horizontal
does, it presents a search/filter box and uses typeahead/autocomplete calls to retrieve results dynamically. This is great for the case where you have maybe 5000 users and want to pick 3 or 4 of them without waiting for 5k<option>
elements to download and render.您可以尝试使用内联模型 -
现在我遇到了另一个问题,找到“render_raw_id_using_this”字段名称。
因此,我转移到 shell 并尝试通过模型查找字段 -
因此,我将
render_raw_id_using_this
替换为fieldname1
类似地,您可以使用这些字段名称来渲染原始 id内联模型中的下拉列表。
You can try using Inline model as such -
Now there is another issue I faced, finding "render_raw_id_using_this" field name.
So, I moved to shell and tried finding fields in through model as such -
So, I replaced
render_raw_id_using_this
withfieldname1
Similarly, you can use these field names to render raw id instead of drop down list in Inline model.
这是一个老问题,但我想在这里为像我一样发现这个问题的人添加一个答案:这种情况正是 Django 内联管理员的用途。具体来说,我将 TabularInlines 与原始 id 字段一起用于具有太多选择的多对多关系。
https://docs.djangoproject.com /en/2.1/ref/contrib/admin/#django.contrib.admin.TabularInline
This is an old question, but I want to add an answer here for people who find this just like I did: this situation is exactly what Django inline admins are for. Specifically, I use TabularInlines with raw id fields for many-to-many relations that have too many choices.
https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.TabularInline