Why are blank and null distinct options for a django model?
Answer a question
When defining fields in a django model, there are two ways to say that the field is allowed to be empty. null means it can be empty in the database, and blank says the field can be empty in a form. Why are these distinct? Every time I set one but not the other something goes wrong. Allowing them to be different seems to be to just inviting problems of the form allowing you to create objects the database won't accept.
In other words, when would you ever use null=True,blank=False or null=False,blank=True in a django model?
Answers
null=False, blank=True is common for CharFields, where a blank answer is stored in the db as a blank string rather than a null. The other way around doesn't make much sense to use.
For non-string fields, a non-answer is stored as a null, and so you need both.
更多推荐

所有评论(0)