2

How to can I work with multiple select options and make these options selected to stay selected ?

Some application? Should I use any JS?

Is possible that my approach may be incomplete or miss something ...?

Context

I have the following model:

class AcademicPeople(models.Model):

    CATHEDRAL_PROFESSOR = 'CATHEDRAL'
    RESEARCH_PROFESSOR = 'RESEARCH'
    INSTITUTIONAL_DIRECTIVE = 'DIRECTIVE'

    OCCUPATION_CHOICES = (
        (CATHEDRAL_PROFESSOR, 'Cathedral Professor'),
        (RESEARCH_PROFESSOR, 'Research Professor'),
        (INSTITUTIONAL_DIRECTIVE, 'Institutional Directive'),
    )

    occupation = models.CharField(
        max_length=255,
        blank = False,
    )

In the forms.py I have:

from .models import
from django.forms.widgets import CheckboxSelectMultiple

class AcademicPeopleForm(forms.ModelForm):
    title = "Details"
    occupation = forms.MultipleChoiceField(
        required=False,
        label='Occupation',
        widget=CheckboxSelectMultiple(),
        choices=AcademicPeople.OCCUPATION_CHOICES
    )

    class Meta:
        model = AcademicPeople
        fields = ('occupation',)

UPDATE

My view is very basic, is the following:

class AccountSettingsUpdateView(LoginRequiredMixin, UpdateView):
    model = AcademicPeople
    form_class = AcademicPeopleForm
    success_url = reverse_lazy('dashboard')
    context_object_name = 'preferences'

When I go to my template using the browser, I get the checkbox select multiple rendered in checkboxes, I select some options and when I save, these options selected does not show their value.

enter image description here

JPRLCol
  • 749
  • 11
  • 28
bgarcial
  • 2,915
  • 10
  • 56
  • 123
  • I've add my view in the **UPDATE** section – bgarcial Apr 20 '17 at 22:20
  • 1
    This should be an error of the rendering of the page. Is the data persisting on the database. I think you error is similar to: https://stackoverflow.com/questions/8948629/keep-selected-value-in-an-html-form-after-submit-is-pressed – JPRLCol Jan 30 '18 at 16:14
  • Did you got solution, Please update Because I stuck with same issue – Dhinakaran Aug 28 '19 at 11:49

0 Answers0