When the user creates a product, multiple actions have to be done in save()
method before calling super(Product,self).save(*args,**kwargs)
.
I'm not sure if I should use just one pre_save
signal to do all these actions or it is better to create a signal for each of these actions separately.
Simple example (I'm going to replace save
overrides by signals):
class Product(..):
def save(...):
if not self.pk:
if not self.category:
self.category = Category.get_default()
if not self.brand:
self.brand = 'NA'
super(Product,self).save(*args,**kwargs)
...
SO
@receiver(pre_save,sender=Product)
def set_attrs(instance,**kwargs):
if kwargs['created']:
instance.category = Category.get_default()
instance.brand = 'NA'
OR
@receiver(pre_save,sender=Product)
def set_category(instance,**kwargs):
if kwargs['created']:
instance.category = Category.get_default()
@receiver(pre_save,sender=Product)
def set_brand(instance,**kwargs):
if kwargs['created']:
instance.brand = 'NA'
This is just simple example. In this case, the general set_attrs
should be probably enough but there are more complex situations with different actions like creating userprofile
for user
and then userplan
etc.
Is there some best practice advice for this? Your opinions?