Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django charfield with default empty string is required in admin

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?

like image 819
Adilet Maratov Avatar asked Mar 24 '26 05:03

Adilet Maratov


1 Answers

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.

like image 106
Daniel Roseman Avatar answered Mar 26 '26 02:03

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!