Javascript 一个更好的 Django Admin ManyToMany 字段小部件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5385933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:58:07  来源:igfitidea点击:

A Better Django Admin ManyToMany Field Widget

javascriptdjangodjango-adminhtml-select

提问by Chris W.

I find the the Django Admin's default models.ManyToManyFieldwidget to be cumbersome to use. It's the HTML selectelement 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 lotof objects of the "other" model it seems to even slows down the rendering of the Admin page.

我发现 Django Admin 的默认models.ManyToManyField小部件使用起来很麻烦。它是HTML 选择元素,如果您有很多“其他”模型的对象,那么实际找到要与“这个”对象关联的“其他”对象是非常不切实际的。如果您有很多“其他”模型的对象,它似乎甚至会减慢管理页面的呈现速度。

I'm aware that I can build my own custom admin widget and apply it to my ManyToManyFieldsas 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?

我知道我可以构建自己的自定义管理小部件并将其应用到我ManyToManyFields认为合适的地方,但是有没有我可以使用的预构建小部件?在我的梦中,我描绘了一个自动完成的文本输入 HTML 小部件。这在 Django 管理框架中是否可行/可能?

Thanks.

谢谢。

回答by Blair

Try using the filter_horizontalattribute on your admin class, for example:

尝试filter_horizontal在您的管理类上使用该属性,例如:

class SomeModelAdmin(admin.ModelAdmin):
    filter_horizontal = ('users',)

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_verticaldoes the same thing with a slightly different layout.

文档所述,“将 ManyToManyField 添加到此列表将改为使用漂亮的不显眼的 JavaScript“过滤器”界面,允许在选项中进行搜索”。filter_vertical用稍微不同的布局做同样的事情。

回答by mrfunyon

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

您可以尝试在管理员中使用原始 ID。和 django 文档: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

如果您正在寻找具有自动完成功能的东西,您可能希望将其视为起点http://code.djangoproject.com/wiki/AutoCompleteSolutions

and finally a very simplistic inline Example:

最后是一个非常简单的内联示例:

models.py

模型.py

class SomeModel(models.Model):
    users = models.ManyToMany(User)

admin.py:

管理.py:

class SomeModelAdmin(admin.ModelAdmin):
    raw_id_fields = ("users",)

回答by Coderer

I haven't actually played with it but I found this promising looking libraryreferenced 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_horizontaldoes, 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.

它似乎完全符合我的要求。而不是加载相关对象的整个列表(无论有多少!)并为您提供选择器以选择其中的一些filter_horizontal,而是提供一个搜索/过滤器框并使用预先输入/自动完成调用来检索结果动态。这对于您可能有 5000 个用户并且想要选择其中的 3 或 4 个而无需等待 5k<option>元素下载和渲染的情况非常有用。

回答by kloddant

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.

这是一个老问题,但我想在这里为那些像我一样发现这个问题的人添加一个答案:这种情况正是 Django 内联管理员的目的。具体来说,我将 TabularInlines 与原始 id 字段一起用于具有太多选择的多对多关系。

https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.TabularInline

https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.TabularInline