Is there a Django ModelField that allows for multi

2020-06-03 00:21发布

问题:

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. What's the quickest way to do this in Django?

回答1:

There is a django snippet which does just this: multiple choice model field. It says:

Usually you want to store multiple choices as a manytomany link to another table. 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.



回答2:

Django-multiselectfield does what you require:

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

It's inspired by the snippet that Alexander mentioned.



回答3:

This django snippet accomplishes the same with a much smaller footprint.

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

make sure to update those imports since they are pre version 1.0