[Django]-Django CharField accepting empty values when it shouldn't

3👍

blank is only used with form validation. If you are having records created with empty values you’ll need to write some custom checks to prevent that.

1👍

Have a look at this answer:

class ClientDebt(models.Model):
    client_id = models.CharField(max_length=255, blank=False, default=None)
    ...
  • blank will prevent an empty string to be provided in your model
  • default=None will set name to None when using something like group = Group(), thus raising an exception when calling save

Alternatively, you may want to have a look at MinLengthValidator.

-1👍

You can set default = '' to your fields

👤Morgan

Leave a comment