Django MultipleChoiceField没有显示已保存的选择

时间:2018-06-30 23:17:28

标签: django multiplechoicefield

这就是我的表单。

class DecisionMadeForm(forms.ModelForm):

  option = forms.MultipleChoiceField(required=False, label = "Which option(s) did you choose? (Please check ALL that apply)")

  class Meta:
     model = Decision_Made
     fields = ('option', )

  def __init__(self, *args, **kwargs):
      dec_id = kwargs.pop("dec_id")
      choicelist = kwargs.pop("choicelist")
      super(DecisionMadeForm, self).__init__(*args, **kwargs)
      instance = getattr(self, 'instance', None)
      query = Solution_Options.objects.filter(dec_id = dec_id, archived = 'N').values_list('option', flat=True).distinct()                       
      query_choices = [(id, id) for id in query] 
      self.fields['sol_option']=forms.MultipleChoiceField(choices=query_choices, required=False, widget=forms.CheckboxSelectMultiple,label="Which choice did you make?")

      if self.instance:
         self.fields['sol_option'].initial = choicelist

在我的views.py

def decision_made(request):  
   try: 
      dec_made = Decision_Made.objects.get(dec_id = dec_id)                                                                                       
   except ObjectDoesNotExist:
      REDIRECT TO DIFFERENT PAGE

    if request.method == 'POST':
       decmadeform = DecisionMadeForm(request.POST, instance=dec_made, dec_id=dec_id, optionlist = dec_made.option)
       if decmadeform.is_valid():
          id = decmadeform.save(commit=False) 
          ...

    else:
        decmadeform = DecisionMadeForm(instance=dec_made, dec_id = dec_id, optionlist = dec_made.option) 

所有选择均已成功存储在数据库中。但是,无论我做什么,我都无法在屏幕上显示它。我尝试了堆栈溢出中给出的所有建议,但没有任何效果。

任何帮助将不胜感激!

0 个答案:

没有答案