DEV Community

Anubhav Singhal
Anubhav Singhal

Posted on • Updated on

Django, Null=True or Blank=True or both

When we add a database field to Django, we have several options for defining the modal field. They usually have default values that we can override according to our needs.

To use modal fields effectively, one must have a thorough understanding of their definitions.


Null

Every column in the database is defined to hold certain attributes. Sometimes we want to keep our columns optional, thus hold a NULL value, and we set these permissions as

NewField = models.charField( max_length=10, null=True)
# This sets the NewField to be an optional field in DB

NewField = models.charField( max_length=10, null=False)
# This sets the NewField to be a mandatory field in DB
Enter fullscreen mode Exit fullscreen mode

Blank

Every Django model field is also defined to hold certain attributes. Sometimes we want to keep our fields blank with no form value, and we set these permissions as:

NewField = models.charField( max_length=10, blank=True)
# This sets the NewField to be allowed to stay blank

NewField = models.charField( max_length=10, blank=False)
# This sets the NewField to not be allowed to stay blank
Enter fullscreen mode Exit fullscreen mode

Null and Blank

By now, it should be clear that Null affects the database table definition and blank affects model validation. There are four ways to use these attributes:

# 1. Requires both
Name = models.charField( max_length=10, null=False, blank=False)

# 2. Requires DB value
Name = models.charField( max_length=10, null=False, blank=True)

# 3. Requires modal value
Name = models.charField( max_length=10, null=True, blank=False)

# 4. Requires none
Name = models.charField( max_length=10, null=True, blank=True)
Enter fullscreen mode Exit fullscreen mode

Let's discuss each distinctively:

1.) Null=False, Blank=False
This is the default condition for modal fields. It means the value is required both in the DB and in models.

2.) Null=False, Blank=True
This is a condition where forms don't require a value, but the field does.

Let's take an example: If your model has a field whose value is being calculated from other models in the field. Then you would want to keep 'Black=True' to not accept values from the user, and 'Null=False' to enforce that a value is always provided.

3.) Null=True, Black=False
This is a condition where form requires a value, but DB doesn't. This is not a very commonly used configuration, I haven't seen one yet. (If you happen to know any, put them down in the comments.)

4.) Null=True, Black=True
This is a condition, where form doesn't require a value, and DB doesn't require a value either. This is pretty self explanatory since the former is reflected with the latter.


You might now have a good idea of what these two attributes mean, and how they are to be used together.
You can read more about them here

Have a good day. Happy Coding.

Top comments (3)

Collapse
 
martinbannister profile image
Martin Bannister-Kelly

3.) Null=True, Black=False

So I have a use case for this, slightly odd one. You could use this set-up where you want user entered data to include certain data, but you are pulling in data from elsewhere that might not conform to these ideal rules.

In my case I have an hourly rate for clients in my model, if a user enters a client using the form I want this hourly rate to be included, there's no occasion it shouldn't be. However I am also pulling existing client information from the Google People API. There is no facility in Google Contacts (other than custom fields that I don't want users to have to create) for an hourly rate, therefore when I'm importing data from the API I don't want to force the DB from the model to require an hourly rate, users will have to add this afterwards, otherwise my data import would fail.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
anubhavitis profile image
Anubhav Singhal

Thanks, Bikaramkeet
Glad you like it.