Search code examples
pythondjangoformscheckboxmany-to-many

How to dynamically show selected option in Django Many-to-Many relation form?


I have a Django form with mant-to-many relation form with checkboxes : which looks like this

And The drop down is something like this: The dropdown in form

For each element in dropdown, I have few preselected options in the checkboxes. I want to display checks in the preselected (pre-mapped) elements .

OR

Do not show the pre-selected 'extraction type' for corresponding 'section' in the checkboxes.

Django: models.py (many to many relation)

(models.py)

class SectionsExtractions(models.Model):

    section_id = models.ForeignKey(Sections, on_delete=models.CASCADE)
    extraction_id = models.ManyToManyField(Extractions)

    def __str__(self):
    return str(self.section_id) + ' - ' + ', '.join([a.extraction_type for a in self.extraction_id.all()])

Django: forms.py

(forms.py)

class SectionExtractionForm(ModelForm):


    extraction_id = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(attrs={'checked' : 'checked'}), queryset=Extractions.objects.all())
    class Meta:
        model = SectionsExtractions
        fields = '__all__'

HTML Template for the form is: Sorry for bad layout.

(HTML)
    {% extends 'cfman/base.html' %}

{% block title %}Section - Extraction{% endblock %}

{% block head %}
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'cfman/assets/css/style.css' %}" />
<script src="{% static 'cfman/assets/js/managerjs.min.js' %}"></script>

{% endblock %}

{% block body %}
<div>
    <h1>Map Section-Extraction</h1>
</div>
<br>
<br>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12 col-md-7">
            <div class="panel panel-default">
                <div class="panel-body">

                <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
                {% csrf_token %}
                {% for field in form %}
<div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
        <span class="text-danger small">{{ field.errors }}</span>
                       </div>
                       <label class="control-label col-sm-2">
                           {{ field.label_tag }}
                       </label>
                     <div class="col-sm-10">
                   {{ field }}
                 </div>

                </div>
                {% endfor %}

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-success">Save</button>
                    </div>
        <a href="{% url 'cfman:add_extraction' %}">
              <i class="fa fa-sitemap fa-1g"></i>&nbsp;
              Add Extraction Type
            </a>

                    </div>
                </form>

                </div>

            </div>
        </div>
    </div>
</div>
{% endblock %}

Solution

  • send instance in form

    form = SectionExtractionForm(
           instance=SectionsExtractions.objects.get(id='<extraction_id>')
    )