如何将python下拉框设为只读?

时间:2019-05-18 19:25:19

标签: python django

如何在python中将下拉框设为只读以仅显示先前选择的选项?

我已经尝试在类中放入ReadOnlyFormMixin,并且对大多数字段都适用。

class ReadOnlyFormMixin(ModelForm):
    def __init__(self, *args, **kwargs):
        super(ReadOnlyFormMixin, self).__init__(*args, **kwargs)
        for key in self.fields.keys():
            self.fields[key].widget.attrs['readonly'] = True

    def save(self, *args, **kwargs):
        # do not do anything
        pass

class ViewPartForm(ReadOnlyFormMixin, ModelForm):

    description = forms.CharField(label='Description',
                                  widget=forms.Textarea(attrs={'rows': 3, 'cols': 35, 'max_length': 800}),
                                  required=True)

    char1 = forms.CharField(label='field2',
                             widget=forms.Textarea(attrs={'rows': 1, 'cols': 25, 'max_length': 800}),
                             required=True)
    char2 = forms.CharField(label='field3',
                             widget=forms.Textarea(attrs={'rows': 1, 'cols': 25, 'max_length': 800}),
                             required=True)

class Manufacturer1Form(ReadOnlyFormMixin, ModelForm):
    manufacturer = forms.ModelChoiceField(queryset=Vendor.objects.filter(vendor_type='manufacturer').order_by('name'))

    class Meta:
        model = Manufacturer1Relationship
        exclude = ('part',)

将ReadOnlyFormMixin放置在第一堂课上,而不是第二堂课。选择字段仍然允许用户更改选择的内容。

0 个答案:

没有答案