Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django check to see if field is blank?

Tags:

django

I retrieved an object from my database. This object has a foreign key field, with the attribute blank=True. How can I check to see if it was actually left blank?

Thanks for the help!

like image 694
Andrew Avatar asked Feb 18 '11 01:02

Andrew


People also ask

How do I check if a field is blank in Django?

If your foreign key field can take null values, it will return None on null, so to check if it was "left blank", you could simply check if the field is None .


1 Answers

blank=True is just telling the admin site that the field can be left blank.

Unless you set null=True as well, your database should complain if you tried to enter a blank value.

If your foreign key field can take null values, it will return None on null, so to check if it was "left blank", you could simply check if the field is None.

>>>   obj.foreignkeyfield is None True   if not obj.foreignkeyfield:     print "This field was left blank" 
like image 138
Yuji 'Tomita' Tomita Avatar answered Sep 29 '22 12:09

Yuji 'Tomita' Tomita