[Answer]-How to make a record read-only in a Django form?

0πŸ‘

βœ…

Yes, it is possible by using parent model form init method sample code:

class VForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(VForm, self).__init__(*args, **kwargs)

        if self.instance.field == 'Y':
            self.fields['field2'] = forms.ChoiceField(
                choices=[(self.instance.field,
                          self.instance.field),], required=True)

This will filter the choice field restricted to only 1 choice, and a blank to select from .In above case β€˜N’ as one of the choice fields is suppressed.
Same is the case for model fields also.

πŸ‘€user956424

1πŸ‘

You can do it using jquery. This is the way at clientside if suitable for you.

You can get value of form fields using id generated by form like(abc_1, abc_2 etc)

then you can compare it with some value and apply css (β€˜disable’,true) to specific field using id.

Leave a comment