Django CheckboxSelectMultiple()小部件 - 保存数据选项后未选择表单小部件值

时间:2017-04-20 21:36:08

标签: python django django-forms django-widget

如何使用多个选择选项并选择这些选项以保持选定状态?

有些申请? 我应该使用任何JS吗?

我的方法可能不完整或 想念什么......?

上下文

我有以下型号:

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,
    )

forms.py我有:

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',)

更新

我的观点非常基本,如下:

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

当我使用浏览器转到我的模板时,我会在复选框中选中多个复选框,我选择了一些选项,当我保存时,选中的这些选项不会显示它们的值。

enter image description here

0 个答案:

没有答案