Reputation: 1382
I have a model with a Charfield:
class SomeModel(models.Model):
charfield = models.Charfield(max_length=5, default="")
...
I expect this field to be not required when using in the admin. But when I include this model to the admin, I have this field required. I could use null=True, blank=True
, but I'm curious Is it some kind of a bug or am I implementing something wrong?
Upvotes: 3
Views: 13534
Reputation: 599530
I'm not sure why you think that's a bug. If you don't specify blank=True
, some kind of non-empty value is required. The empty string, by definition, is an empty value; so Django quite correctly displays an error telling you to supply an actual value.
You do need to specify blank=True
if you want the empty string to be allowed. But you don't need to specify null=True
; indeed, as the docs show, you shouldn't use that on CharFields.
Upvotes: 9
Reputation: 319
It's correct. default
, null
and blank
have different meanings.
null
allows NULL value on database https://docs.djangoproject.com/en/dev/ref/models/fields/#null
blank
allows blank value on the model https://docs.djangoproject.com/en/dev/ref/models/fields/#blank
Additionally, a ModelForm (used by admin) with blank=True
sets the required field as false, see https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#field-types
Upvotes: 1