The Django docs say:
Avoid using null on string-based fields such as CharField and TextField because empty string values will always be stored as empty strings, not as NULL.
So I set up my model like
class Address(models.Model):
address_num = models.TextField()
street_predir = models.CharField(max_length=2, default='') # optional
street_name = models.TextField()
street_suffix = models.CharField(max_length=4, default='') # optional
street_postdir = models.CharField(max_length=2, default='') # optional
and then create an object that's missing the optional fields
raw_address = '1234 MAIN ST'
# Run this through an address parser - this returns a dictionary of address components
# Optional fields are returned as None if not present
components = parser.parse(raw_address)
address = Address(**components)
address.save()
But I'm getting the error
null value in column "street_predir" violates not-null constraint
DETAIL: Failing row contains (8633570, 2057 ANCHOR ST, 2057, null, ANCHOR, ST, null, null, null, ANCHOR ST, null, null, )
Can anyone explain why this isn't defaulting to an empty string? FWIW I'm using Django 1.7.4 and a Postgres DB.
default='', you should rather useblank=Trueto allow empty values in forms. Can you post the traceback and possibly the contents offrom django.db import connection; print(connection.queries)? - knbkNoneinstead of an empty string. - knbk