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

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

问题描述

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

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

如何在我自己的应用程序中使用这个小部件?我没有看到像这样的这里列出的任何内容a>。

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

推荐答案

docs


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天全站免登陆