Missing required Charfield in django is saved as an empty string and does not cause an error

If I try to save an instance of an incomplete model in Django 1.10, I expect Django to raise an error. This does not seem to be the case.

models.py:

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)

So, I have two fields that are not allowed to be empty (the default behavior, the restriction NOT NULLapplies when creating a Django table when creating MySQL). I expect Django to break the error if one of the fields is not set before saving.

However, when I create an incomplete instance, the data is stored just fine:

>>> from test.models import Essai
>>> bouh = Essai()
>>> bouh.ch1 = "some content for ch1"
>>> bouh.save()
>>> bouh.id
9
>>> bouh.ch1
'some content for ch1'
>>> bouh.ch2
''
>>> 

I would expect Django to raise an error. However, if I force ch2- None, it raises an error:

>>> bouh = Essai()
>>> bouh.ch1 = "some content for ch1"
>>> bouh.ch2 = None
>>> 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
>>> bouh.id
>>> bouh.ch1
'some content for ch1'
>>> bouh.ch2
>>>

? - ? Django ?

!

EDIT

, , SQL "" NULL, Django model blank = False ? , ModelForm, , django doc .

. : Charfield django Modelform

0
1

,

from django.db import models

class Essai(models.Model):
    ch1 = models.CharField(max_length=100)
    ch2 = models.CharField(max_length=100)
0

Source: https://habr.com/ru/post/1656049/


All Articles