Search code examples
pythonmysqldjangomodels

how to establish in models file that a django model should be defined as NOT NULL?


In my models.py file I would like a way to explicitly state that certain fields in my model should be defined as NOT NULL such that the MySQL code would be

CREATE TABLE Minion_job(MyColumn VARCHAR(50) NOT NULL, ...., ....);

Instead of what it does right now which is

CREATE TABLE Minion_job(MyColumn VARCHAR(50), ...., ....);

I was able to find that models have special optional fields, most notably in this case blank and null, for these kinds of modifications but I wasn't able to figure out based on my googling how to implement those fields (or otherwise achieve the desired effect) in my models.py file.


Solution

  • You mean:

    class sth(models.Model):
        somefield = models.CharField(max_length = 20, null = False, blank = False) 
    

    :) Glad that I could help.