添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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 ?

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

Do I have to use a list directly in my main model ?
class My_Main(models.Model):
GENDER_CHOICES = [
(‘Yellow’,‘Yellow’),
(‘Red’,‘Red’),

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 :

  • How to create the “color” manytomany default relation ?
  • 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>
    							<th class="align-middle text-center">Name</th>
    							<th class="align-middle text-center">Check</th>
    					</thead>
    					<tbody>
    						{% for my_color in my_colors %}
    								<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">
    									{% else %}
    										<div class="form-check">
    											<input class="form-check-input" type="checkbox" value={{my_color.id}} name="boxes">
    									{% endif %}
    						{% endfor%}
    					</tbody>
    				</table>
    				<button class="btn btn-secondary" name="btn_color" type="submit">Save</button>
    			</form>
                  

    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 ?