Full fields in django form

I use modelformset factory to create a set of forms from model fields. Here I want to make only request objects as readonly and others (additional forms) as fields without reading

How can i achieve this?

  AuthotFormSet = modelformset_factory(Author, extra=2,)
  formset = AuthorFormSet(queryset=Author.objects.all())

In Above formset, I wanted to display all the request objects as readonly, and the remaining additional forms as fields that were not readonly. How can i achieve this?

if i used

      for form in formset.forms:
          form.fields['weight'].widget.attrs['readonly'] = True

This will convert all forms (including optional ones) to readonly, which I don't want. And also I use jquery plugin to dynamically add form to formet

+5
source share
2 answers

, , , .

#forms.py
class AuthorForm(forms.ModelForm):
    class Meta:
        model = Author

    def __init__(self, *args, **kwargs):
        super(AuthorForm, self).__init__(*args, **kwargs)
        if self.instance.id:
            self.fields['weight'].widget.attrs['readonly'] = True

#views.py
AuthorFormSet = modelformset_factory(Author, extra=2, form=AuthorForm)
+7

python . . jquery

$('.class').attr('readonly', true);

$('#id').attr('readonly', true);
+1

All Articles