[Answer]-Django – Get I/O error on changing object attribute

1πŸ‘

βœ…

The trick is to add an if condition in your save method and check if it is necessary to read the whole code in the save function.

For this you add a function named, has_changed

def has_changed(instance, field, manager='objects'):
    """Returns true if a field has changed in a model

    May be used in a model.save() method.

    """
    if not instance.pk:
        return True
    manager = getattr(instance.__class__, manager)
    old = getattr(manager.get(pk=instance.pk), field)
    return not getattr(instance, field) == old

And you use it in the model definition like this:

if has_changed(self, 'backgroundpicture'):

       if self.backgroundpicture.width >= self.backgroundpicture.height:
           self.landscape=True
...
πŸ‘€jibe

Leave a comment