title: Django – Disabling ModelForm fields
body: <p>How is it possible to disable certain ModelForm fields to indicate that they're not editable? The <a href="https://docs.djangoproject.com/en/3.1/ref/forms/fields/#disabled" rel="nofollow noreferrer">docs</a> seem to suggest setting the widget to <code>disabled</code> should do the trick, including making it tamper-resistent, however doing that form validation fails as the disabled fields POST as empty. (also trying to use the <code>has_changed</code> method, but doesn't seem to work for ModelForms and no equivalent is provided...)</p>
  <p>I alternatively tried setting the <code>clean_&lt;fieldname&gt;</code> method on ModelForm level, to set the field to the referenced instance's value, but validation fails before that's reached.</p>
  <p>Some sample code:</p>
  <pre class="lang-py prettyprint-override"><code># forms.py
  class MyForm(forms.ModelForm):
      class Meta:
          model = MyModel
          fields = '__all__'
          widgets = {
              'slug': forms.TextInput(attrs={
                  'disabled': True
              })
          }
  
  
  # views.py
  def edit_mymodel(request, slug: str=None):
      instance = get_object_or_404(MyModel, slug=slug)
      form_myform = MyForm(request.POST or None, instance=instance)
  
      if request.method == 'POST':
          if form_myform.is_valid():
              # fails, because slug is empty in POST request
              pass
  </code></pre>