How can you create a non-empty CharField in Django?

another option that doesn’t require you to manually call clean is to use this:

name = models.CharField(max_length=100, blank=False, default=None)
  • blank will prevent an empty string to be provided in the admin or using a form or serializer (most cases). However as pointed out in the comments, this unfortunately does not prevent things like model.name = "" (manually setting blank string)
  • default=None will set name to None when using something like group = Group(), thus raising an exception when calling save

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)