Django MultipleChoiceField not showing saved choices
Django MultipleChoiceField not showing saved choices
This is what my form looks like.
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
In my 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)
All the choices are stored successfully in the database. But no matter what I do I cannot display it on the screen. I have tried all the suggestions given in stack overflow and nothing has worked.
Any help would be much appreciated!
forms.ModelMultipleChoiceField
forms.MultipleChoiceField
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
use this
forms.ModelMultipleChoiceFieldinsteaf offorms.MultipleChoiceField– Yusef BH
Jul 1 at 0:17