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?
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.
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 .
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.
Yes, this way:
ModelName._meta.get_field('field_name').null
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