title: Don't require field based on another field's value
body: <p>I have a Django ModelForm where I want one field to be treated as conditionally required. I want it to be required by default, but not required depending on the value of another field. My model looks something along the lines of:</p>
  
  <pre><code>class MyModel(models.Model):
      foo = models.BooleanField()
      bar = models.CharField(choices=(('A', 'A'),('B', 'B'),('C', 'C')),)
  </code></pre>
  
  <p>Neither have <code>null=True</code> or <code>blank=True</code> so both appear as "required" when I render my form, which is what I want. </p>
  
  <p>However, I have some javascript on my form template that hides the <code>bar</code> field input depending on the value of the input for <code>foo</code>. In this case, when the form is submitted, I don't want to have any validation errors about <code>bar</code> being required. </p>
  
  <p>I am trying to implement this logic in my ModelForm's <code>clean</code> method, like so:</p>
  
  <pre><code>def clean(self):
      data = super(MyModelForm, self).clean()
      if data.get('foo') == True and 'bar' in self.errors:
          del self.errors['bar']
      return data
  </code></pre>
  
  <p>However, this still seems to give me a "This field cannot be blank." error. I've noticed that my ModelForm also has an <code>_errors</code> dict, but if I try adding <code>del self._errors['bar']</code> I end up with an exception. </p>
  
  <p>I have found some ways to do the opposite of what I want(<a href="https://stackoverflow.com/questions/2306800/django-form-validation-making-required-conditional">1</a>, <a href="https://stackoverflow.com/questions/10967646/django-form-field-required-conditionally">2</a>), which is have the field not be required by default and then check it should be required in some cases in <code>clean</code>, but this is not what I want. </p>
  
  <p>I've also tried adding <code>data['bar'] = "  "</code> as a workaround but then I get an error about not choosing one of the choices. Ideally, I'd rather just be able to save the empty string. </p>