2016-12-15 46 views
0

我與複選框MANT一對多的關係形式的Django的形式: which looks like this如何在Django多對多關係表中動態顯示選定的選項?

和下拉是這樣的:The dropdown in form

對於下拉每一個元素,我有幾個預選方案複選框。我想在預選的(預映射)元素中顯示檢查。

OR

不顯示,並顯示預選「提取類型」在複選框對應的「節」。

Django的:models.py(多對多關係)

(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模板的形式是: 對不起,壞的佈局。

(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 %} 

回答

0

發送instance形式

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

這個有意見要加入? –

+0

**錯誤**:int()與基數10的無效文字:'' –

+0

它應該是對象的實際ID。 –

相關問題