Volodymyr Kirichinets Volodymyr Kirichinets - 1 year ago 200
HTML Question

Django how to ManyToManyField show in template as Select widget

My models.py:

class Model2(models.Model)
choice_field = models.ManyToManyField(to=Model)

my template.html:

{{ form.choice_field }}

I get a values list and can a scroll and choose what I want. But i want to use Select with options for this values in template.

Answer Source

You can define widget for each field of your model manually. Assuming you using built-in ModelForm your code in forms.py can be something like this:

from django.forms import ModelForm, Select

class Model2Form(ModelForm):
    class Meta:
        model = Model2
        fields = [
        widgets = {
            'choice_field': Select(),
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download