skip to Main Content

After updating an existing django field to have a default value if I save the field with a null value I get the error: IntegrityError: null value in column

rather than the field being set to the default, does anyone know why this may be? If I include null=True it just sets it to null.

2

Answers


  1. I think by explicitly trying to save the field as null you are overriding the default value. Try and update the object only if the variable exists.

    my_variable = None
    
    if my_variable:
        my_object.my_field = my_variable
    

    if you post more of your code i could help more

    Login or Signup to reply.
  2. class YourModel(models.Model):
          your_given_field = models.CharField(max_length=100, default='default_value')
    
    def save(self, *args, **kwargs):
        if not self.your_given_field:
            self.your_given_field = 'default_value'
        super().save(*args, **kwargs)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search