Multiple Choice with CheckBox

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

See the 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.

Hi Ken,

Thanks for your help, I try your first solution (the best one …:wink:
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:

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:

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

template.html

			<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>

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.

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 :

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 ?

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.

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 ?

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?

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 …

Thanks for your help Ken