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

后端 未结 5 2021
情话喂你
情话喂你 2020-12-14 06:31

I have a simple model which looks like this:

class Group(models.Model):
    name = models.CharField(max_length = 100, blank=False)

I would

5条回答
  •  不知归路
    2020-12-14 06:44

    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 your model
    • default=None will set name to None when using something like group = Group(), thus raising an exception when calling save

提交回复
热议问题