Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django python blank=True still required [duplicate]

In the code below, somehow the fields that have blank=True are still required and cannot be Null

Example:

# Skill train timer
    class SkillTrainingTimer(models.Model):
    """
    Character Training timer,  keep track fo what skill is in training, and when it is done
    """
    character = models.ForeignKey(Character, unique=True)
    skill = models.ForeignKey(Skill, blank=True)
    trainingground = models.ForeignKey(TrainingGround, verbose_name='Training ground')
    timer = models.DateTimeField()

and now i do:

train_skill = SkillTrainingTimer(
        character=some_character_obj,
        trainingground=some_trainingground_obj,
        timer = fatetime.datetime.now()
)
train_skill.save()

It starts complaining skill cannot be Null. I have this in a lot of models and functions and so far i just dumped some useless data in it.

like image 809
Hans de Jong Avatar asked Sep 04 '25 16:09

Hans de Jong


2 Answers

null=True sets NULL on the column in your DB.

blank=True determines whether the field will be required in forms. This includes the admin and your own custom forms. If blank=True then the field will not be required, whereas if it's False the field cannot be blank. Hope this helps!

like image 135
Arovit Avatar answered Sep 07 '25 16:09

Arovit


You have to add the null=True argument on the field definition, preferably before a syncdb (or you can migrate with South)

skill = models.ForeignKey(Skill, blank=True, null=True)
like image 39
Steve K Avatar answered Sep 07 '25 16:09

Steve K