I have an 'address' field in my Django model that I want to be optional:
class Home(models.Model):
address = models.CharField(max_length=256, blank=True, default=None)
city = models.CharField(max_length=25)
I want this to be an optional field in my database but I didn't set "null=True" because the documentation says
"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. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL."
However, when I create a Home object with this command:
Home.objects.create(city='Chicago')
I get an error:
IntegrityError: null value in column 'address' violates not-null constraint.
Shouldn't I add "null=True" to the address field despite what the docs say in order to make it optional? If not, how should it be declared so that I can create new Home objects without raising this error?