Personally I like using signals:
from django.db import models
from django.db.models.signals import pre_save
class MyModel(models.Model):
...
def custom_action_before_saving(sender, instance, *args, **kwargs):
...
pre_save.connect(custom_action_before_saving, sender=MyModel)
But I wonder if there're some times or task when is better override the save method in a model class:
from django.db import models
class MyModel(models.Model):
...
def save(self):
...
super(MyModel, self).save()
I am asking this because there's an example of overriding the save()
method (link provided above) in Django's Documentation page, so I don't think it's a bad practice.
Let's take pre_save()
as example, docs says:
This is sent at the beginning of a model’s save() method.
Does it means that overriding save
has the same effect over performance that using signals?
if self....:
overriding save to avoid code executions in some cases? – Arad