Django Multiple Choice Field / Checkbox Select Multiple

前端 未结 6 1167
清酒与你
清酒与你 2020-11-29 18:09

I have a Django application and want to display multiple choice checkboxes in a user\'s profile. They will then be able to select multiple items.

This is a simplifi

6条回答
  •  Happy的楠姐
    2020-11-29 19:06

    You can easily achieve this using ArrayField:

    # in my models...
    tags = ArrayField(models.CharField(null=True, blank=True, max_length=100, choices=SECTORS_TAGS_CHOICES), blank=True, default=list)
    
    # in my forms...
    class MyForm(forms.ModelForm):
    
        class Meta:
            model = ModelClass
            fields = [..., 'tags', ...]
    

    I use tagsinput JS library to render my tags but you can use whatever you like: This my template for this widget:

    {% if not hidelabel and field.label %}{% endif %}
    
    {% if field.help_text %}{{ field.help_text | safe }}{% endif %}
    

提交回复
热议问题