What is the difference between this two fields in a Django app? What behaviour should I expect?
field_a = CharField(max_length=50, verbose_name='field_a', blank=True)
field_b = CharField(max_length=50, verbose_name='field_b', blank=True, default='')
In Very simple words, Blank is different than null. null is purely database-related, whereas blank is validation-related(required in form). If null=True , Django will store empty values as NULL in the database . If a field has blank=True , form validation will allow entry of an empty value .
The fundamental difference between these two is that null controls the validation at the database level, and blank is used during form field validation at the application level. By default all fields are required. In order to make a field optional, we have to say so explicitly.
CharField is a string field, for small- to large-sized strings. It is like a string field in C/C+++. CharField is generally used for storing small strings like first name, last name, etc. To store larger text TextField is used. The default form widget for this field is TextInput.
null. If True , Django will store empty values as NULL in the database. Default is False .
If default
value is not given, empty string is used for CharField
according to the following code (taken from django/db/models/fields/__init__.py
source):
def get_default(self):
"""
Returns the default value for this field.
"""
if self.has_default():
if callable(self.default):
return self.default()
return self.default
if (not self.empty_strings_allowed or (self.null and
not connection.features.interprets_empty_strings_as_nulls)):
return None
return ""
So they should behave same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With