Django 多选小部件? [英] Django multi-select widget?

查看:35
本文介绍了Django 多选小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 管理站点使用了一个非常酷的小部件:

The Django admin site makes use of a really cool widget:

如何在我自己的应用程序中使用这个小部件?我没有看到类似的内容列在此处.

How can I make use of this widget in my own applications? I don't see anything like that listed here.

推荐答案

来自 文档:

Django Admin 应用程序为日历、过滤选择等定义了许多自定义小部件.这些小部件定义了媒体要求,Django Admin 使用自定义小部件代替 Django 默认设置.管理模板将仅包含在任何给定页面上呈现小部件所需的那些媒体文件.

The Django Admin application defines a number of customized widgets for calendars, filtered selections, and so on. These widgets define media requirements, and the Django Admin uses the custom widgets in place of the Django defaults. The Admin templates will only include those media files that are required to render the widgets on any given page.

如果您喜欢 Django Admin 应用程序使用的小部件,请随时在您自己的应用程序中使用它们!它们都存储在 django.contrib.admin.widgets 中.

If you like the widgets that the Django Admin application uses, feel free to use them in your own application! They’re all stored in django.contrib.admin.widgets.

在这种情况下,您需要 FilteredSelectMultiple 小部件.要使用它,请在表单字段上应用小部件,如下所示:

In this case, you want the FilteredSelectMultiple widget. To use it, apply the widget on a form field like so:

my_field = forms.ModelMultipleChoiceField(queryset=MyModel.objects.all(), widget=FilteredSelectMultiple("verbose name", is_stacked=False))

确保在模板中包含表单media,因为它需要包含一些 JS 文件.

Make sure to include the forms media in the template as it needs to include a few JS files.

这篇关于Django 多选小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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