# Multiple Choice with CheckBox

**URL:** <https://forum.djangoproject.com/t/multiple-choice-with-checkbox/14907>\
**Category:** Forms & APIs\
**Created:** [July 20, 2022, 10:29am UTC](https://forum.djangoproject.com/t/multiple-choice-with-checkbox/14907 "2022-07-20T10:29:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![PierreFalque](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pierrefalque/32/7368_2.png) [@PierreFalque](https://forum.djangoproject.com/u/PierreFalque)\
**Post date:** [July 20, 2022, 10:29am UTC](https://forum.djangoproject.com/t/multiple-choice-with-checkbox/14907/1 "2022-07-20T10:29:53Z")

</div>

Hi Everyone,

I am trying to add a multiple choice of color for my objects.  
And to do it with a Checkbox  
But I dont know what is the best way to do it … perhaps, you can help me ?

1. Do I list all the color in a foreign model ? This can be mofify by the user without enter in the code.  
class Settings\_Color(models.Model):  
name = models.CharField(max\_length = 20, blank=True)  
check = models.BooleanField(default=False)  
def **str** (self):  
return self.name

2. Do I have to use a list directly in my main model ?  
class My\_Main(models.Model):  
GENDER\_CHOICES = [  
(‘Yellow’,‘Yellow’),  
(‘Red’,‘Red’),  
]

3. Depend on the choice,  
4.a. Instance creation in views.py ? How to do ?  
4.b. How a display this in my template ?  
4.c. How to save the result in the model ?

Thanks for your help …

Pierre

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 20, 2022, 11:07am UTC](https://forum.djangoproject.com/t/multiple-choice-with-checkbox/14907/2 "2022-07-20T11:07:13Z")

</div>

See the [`CheckboxSelectMultiple`](https://docs.djangoproject.com/en/4.0/ref/forms/widgets/#checkboxselectmultiple) widget.

You can store the selected choices in many different ways:  
(These are listed roughly in the order that I would suggest them, best to worst.)

- Many-to-many field between the base model and the model defining the valid choices.

- As entries in a related table with a ForeignKey to the base table.

- As elements of a JSONField or PostgreSQL ArrayField

- As a character string with a delimiter between elements.

There are probably some other options as well, but these are what I’ve seen and used most often.

---

<div class="post-metadata">

**Author:** ![PierreFalque](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pierrefalque/32/7368_2.png) [@PierreFalque](https://forum.djangoproject.com/u/PierreFalque)\
**Post date:** [July 20, 2022, 12:35pm UTC](https://forum.djangoproject.com/t/multiple-choice-with-checkbox/14907/3 "2022-07-20T12:35:28Z")

</div>

Hi Ken,

Thanks for your help, I try your first solution (the best one …😉  
But just 2 questions :

1. How to create the “color” manytomany default relation ?
2. How to get the user value (the boxes he check) and save it into the models

Another thanks

So, what I does:

**models.py:**

```auto
class Color(models.Model):
	name = models.CharField(max_length = 20, blank=True)
	check = models.BooleanField(default=False)

class Main(models.Model):
	color = models.ManyToManyField(Color, related_name="color")

```

Views.py:

```auto
def the_main(request,my_id):
	my_main = Main.objects.get(id=my_id)
	my_colors = my_main.color.all()

```

template.html

```auto
			<form method="POST" enctype="multipart/form-data">
				{% csrf_token %}
				<table id="table" class="table dt-responsive">
					<thead>
						<tr>
							<th class="align-middle text-center">Name</th>
							<th class="align-middle text-center">Check</th>
						</tr>
					</thead>
					<tbody>
						{% for my_color in my_colors %}
							<tr>
								<td class="align-middle text-center">{{ my_color.name }}</td>
								<td class="align-middle text-center">
									{% if my_color.check %}
										<div class="form-check">
											<input class="form-check-input" type="checkbox" checked="checked" value={{my_color.id}} name="boxes">
										</div>
									{% else %}
										<div class="form-check">
											<input class="form-check-input" type="checkbox" value={{my_color.id}} name="boxes">
										</div>
									{% endif %}
								</td>
							</tr>
						{% endfor%}
					</tbody>
				</table>

				<button class="btn btn-secondary" name="btn_color" type="submit">Save</button>
			</form>

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 20, 2022, 12:48pm UTC](https://forum.djangoproject.com/t/multiple-choice-with-checkbox/14907/4 "2022-07-20T12:48:09Z")

</div>

If you create a model form with that as a form field, and have the template render it as a form, then it functions just like any other form field.

---

<div class="post-metadata">

**Author:** ![PierreFalque](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pierrefalque/32/7368_2.png) [@PierreFalque](https://forum.djangoproject.com/u/PierreFalque)\
**Post date:** [July 20, 2022, 4:28pm UTC](https://forum.djangoproject.com/t/multiple-choice-with-checkbox/14907/5 "2022-07-20T16:28:22Z")

</div>

Hmmmm, I think, I doing something wrong with the Form management …  
The code is ok, I can display the list, the checkbox information are display and saved … but it is the same color set (the last one saved) for all my main object.

I add this in forms.py :

```auto
class Color_Form(ModelForm):
	class Meta:
		model = Color
		fields = ' __all__'

```

and in views.py, I replace the ‘my\_colors’ affectation with :  
`my_colors = Color_Form(instance=my_main)`

But the result display is only the labels of color : ‘name’ and ‘check’  
and not all the colors to select them …

Another idea ?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 20, 2022, 4:30pm UTC](https://forum.djangoproject.com/t/multiple-choice-with-checkbox/14907/6 "2022-07-20T16:30:46Z")

</div>

The form should be for the instance of Main for which you are trying to select a set of Color. You are editing an instance of Main, trying to associate that instance with one or more instances of Color.

---

<div class="post-metadata">

**Author:** ![PierreFalque](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pierrefalque/32/7368_2.png) [@PierreFalque](https://forum.djangoproject.com/u/PierreFalque)\
**Post date:** [July 20, 2022, 8:10pm UTC](https://forum.djangoproject.com/t/multiple-choice-with-checkbox/14907/7 "2022-07-20T20:10:59Z")

</div>

I try a lot a things but with no result, I think, I don’t really understand what to do.  
Perhaps, do you have a small example to help me understand ?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 20, 2022, 8:18pm UTC](https://forum.djangoproject.com/t/multiple-choice-with-checkbox/14907/8 "2022-07-20T20:18:59Z")

</div>

What do you think is causing the issue here? Are you ok with a regular form and it’s just the many-to-many relationship that is causing a problem? Or are you not understanding how a form works?

---

<div class="post-metadata">

**Author:** ![PierreFalque](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pierrefalque/32/7368_2.png) [@PierreFalque](https://forum.djangoproject.com/u/PierreFalque)\
**Post date:** [July 21, 2022, 7:54am UTC](https://forum.djangoproject.com/t/multiple-choice-with-checkbox/14907/9 "2022-07-21T07:54:04Z")

</div>

I found a youtube serie who explain this and help me to solve my problem.  
I give here the link if it can help other …

[![](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/2/2d687d8d7e338c158a15f669000d7ffac457d399.jpeg "DJANGO – 22 ADDING A CHOICE FIELD") ](https://www.youtube.com/watch?v=iddMtHAV_N0)

Thanks for your help Ken
