我有一种情况,当我的一个模型被保存时,MyModel
我想检查一个字段,并在任何其他具有相同功能的模型中触发相同的更改some_key.
该代码工作正常,但它递归调用信号。结果,我浪费了 CPU/DB/API 调用。我基本上想在.save().
任何建议期间绕过信号?
class MyModel(models.Model):
#bah
some_field = #
some_key = #
#in package code __init__.py
@receiver(models_.post_save_for, sender=MyModel)
def my_model_post_processing(sender, **kwargs):
# do some unrelated logic...
logic = 'fun! '
#if something has changed... update any other field with the same id
cascade_update = MyModel.exclude(id=sender.id).filter(some_key=sender.some_key)
for c in cascade_update:
c.some_field = sender.some_field
c.save()