Followup : missing required Charfield in django Modelform is saved as empty string and do not raise an error
<p>If I try to save incomplete model instance in Django 1.10, I would expect Django to raise an error. It does not seem to be the case.</p>

<p>models.py:</p>

<pre><code>from django.db import models

class Essai(models.Model):
    ch1 = models.CharField(max_length=100, blank=False)
    ch2 = models.CharField(max_length=100, blank=False)
</code></pre>

<p>So I have two fields not allowed to be empty (default behavior, <code>NOT NULL</code> restriction is applied by Django at MySQL table creation). I expect Django to rase an error if one of the fields is not set before storing.</p>

<p>However, when I create an incomplete instance, the data is stored just fine:</p>

<pre><code>&gt;&gt;&gt; from test.models import Essai
&gt;&gt;&gt; bouh = Essai()
&gt;&gt;&gt; bouh.ch1 = "some content for ch1"
&gt;&gt;&gt; bouh.save()
&gt;&gt;&gt; bouh.id
9
&gt;&gt;&gt; bouh.ch1
'some content for ch1'
&gt;&gt;&gt; bouh.ch2
''
&gt;&gt;&gt; 
</code></pre>

<p>I would have expected Django to raise an error. If I force <code>ch2</code> to <code>None</code>, however, it raises an error:</p>

<pre><code>&gt;&gt;&gt; bouh = Essai()
&gt;&gt;&gt; bouh.ch1 = "some content for ch1"
&gt;&gt;&gt; bouh.ch2 = None
&gt;&gt;&gt; bouh.save()
Traceback (most recent call last):
  (...)
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: test_essai.ch2
&gt;&gt;&gt; bouh.id
&gt;&gt;&gt; bouh.ch1
'some content for ch1'
&gt;&gt;&gt; bouh.ch2
&gt;&gt;&gt;
</code></pre>

<p>Explanation: Django is not raising an error as default behavior in this simple case because in SQL empty string <code>""</code> is not equivalent to NULL, as stated in <a href="http://stackoverflow.com/questions/17816229/django-model-blank-false-does-not-work">Django model blank=False does not work?</a></p>

<p>Now, if we look at ModelForm behavior, there seem to be a inconsistency in django doc:</p>

<p>According to:
 <a href="https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/#selecting-the-fields-to-use" rel="nofollow">https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/#selecting-the-fields-to-use</a></p>

<blockquote>
  <p>Django will prevent any attempt to save an incomplete model, so if the model does not allow the missing fields to be empty, and does not provide a default value for the missing fields, any attempt to save() a ModelForm with missing fields will fail. To avoid this failure, you must instantiate your model with initial values for the missing, but required fields:(â¦)</p>
</blockquote>

<p>a modelForm should not be saved with a missing field if there is no default value. </p>

<p>So with this ModelForm:
<code>
class EssaiModelForm(forms.ModelForm):
    class Meta:
        model = Essai
        fields = ['ch1']
</code>
A form with only one field <code>ch1</code> is generated. </p>

<ul>
<li>If <code>ch1</code> is left empty, the validation fails at <code>EssaiModelFormInstance.is_valid()</code> as expected. </li>
<li>If <code>ch1</code> contains a value, the validation succeeds even though <code>ch2</code> is still missing. Then the EssaiModelFormInstance.save() succeeds, contrary to what is claimed in django documentation. <code>ch2</code> is empty string and thus is compatible with SQL requirements (<code>NOT NULL</code>).</li>
</ul>

<p>So it seems that there is a default default value empty string <code>""</code> for Charfield that is <strong>not accepted</strong> in form validation but that is <strong>accepted</strong> in save() validation. This may require clarification in the documentation.</p>

<p>As the two fields <code>ch1</code> and <code>ch2</code> are always required in your case, all you need to do is modify your model such that </p>

<pre><code>from django.db import models

class Essai(models.Model):
    ch1 = models.CharField(max_length=100)
    ch2 = models.CharField(max_length=100)
</code></pre>

<p>and include the fields in the form as</p>

<pre><code>class EssaiModelForm(forms.ModelForm):
class Meta:
    model = Essai
    fields = ['ch1', 'ch2']
</code></pre>

<p>Django will automatically raise a validation error for this fields on <code>is_valid()</code></p>

<p>Or if you want to disallow empty string inputs to these fields <code>blank=False</code> will not work</p>

<p>Refer this <a href="http://stackoverflow.com/questions/6194988/django-how-to-set-blank-false-required-false">Django - how to set blank = False, required = False</a></p>

python mysql django validation django-models