Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a field allows null values in Django

I'm writing generic functions (using hasattr, setattr, getattr ...), in order to manage and update field values for a given field field_name of a certain model?

Is there a way to check if the field my_model.field_name allows null values?

like image 601
dolma33 Avatar asked Mar 22 '12 15:03

dolma33


People also ask

How check field is null in Django?

null=True will make the field accept NULL values. Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the database.

Can CharField be null?

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 .

Is null false Django?

The Django convention is to use the empty string, not NULL. The default values of null and blank are False. Also there is a special case, when you need to accept NULL values for a BooleanField , use NullBooleanField instead.


1 Answers

Yes, this way:

ModelName._meta.get_field('field_name').null
like image 95
kosii Avatar answered Sep 20 '22 11:09

kosii