简体   繁体   English

除了ManyToMany之外,是否有允许多种选择的Django ModelField?

[英]Is there a Django ModelField that allows for multiple choices, aside from ManyToMany?

I'd like the user to be able to make multiple selections via the admin interface, and store the result as a list of comma-separated values. 我希望用户能够通过管理界面进行多项选择,并将结果存储为逗号分隔值列表。 A select-multiple or a list of checkboxes would be great. 选择多个或复选框列表会很棒。 However, I don't need the items in this list of values to refer to any models in particular... I just want a text list of items, plain and simple, hence I don't think the ManyToManyField is the one I'm looking for. 但是,我不需要这个值列表中的项目来特别引用任何模型......我只想要一个简单明了的项目文本列表,因此我不认为ManyToManyField是我的那个我正在寻找。 What's the quickest way to do this in Django? 在Django中最快的方法是什么?

There is a django snippet which does just this: multiple choice model field . 有一个django片段就是这样做的: 多选模型字段 It says: 它说:

Usually you want to store multiple choices as a manytomany link to another table. 通常,您希望将多个选项存储为另一个表的manytomany链接。 Sometimes however it is useful to store them in the model itself. 然而,有时将它们存储在模型本身中是有用的。 This field implements a model field and an accompanying formfield to store multiple choices as a comma-separated list of values, using the normal CHOICES attribute. 此字段使用常规CHOICES属性实现模型字段和附带的表单字段,以将多个选项存储为逗号分隔的值列表。

Django-multiselectfield does what you require: Django-multiselectfield可满足您的需求:

https://github.com/goinnn/django-multiselectfield https://github.com/goinnn/django-multiselectfield

It's inspired by the snippet that Alexander mentioned. 它的灵感来自亚历山大提到的片段。

This django snippet accomplishes the same with a much smaller footprint. 这个django片段以更小的占地面积实现了这一目标。

http://djangosnippets.org/snippets/23/ http://djangosnippets.org/snippets/23/

make sure to update those imports since they are pre version 1.0 确保更新这些导入,因为它们是1.0版之前的版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM