Home > Blockchain >  How do I set a NOT NULL constraint on Django charfield?
How do I set a NOT NULL constraint on Django charfield?

Time:10-21

I'm struggling with how I can set a NOT NULL constraint on a charfield.

Currently, my model looks like this:

class Tutors(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20, blank=False)
    email = models.EmailField(max_length=254)
    birth_day = models.DateField(auto_now=False, auto_now_add=False)

    def __str__(self):
        return(self.first_name   ' '   self.last_name)

I want to make sure that last_name and first_name can't be saved to the database as NOT NULL. Docs are confusing me a bit lol

CodePudding user response:

If you don't specify null in your model class. null=False is default

Django Documentation

CodePudding user response:

blank=False will enforce that any form for this model cannot have a blank value for that field. To set NOT NULL on the underlying database column, also include null=False.

  • Related