如何通过.save()方法将kwargs参数发送到post_save信号



我有两个模型,Process和Notification,我需要在保存流程时创建一个通知,所以我使用postrongave信号:

def create_notification(sender, instance, *args, **kwargs):
if instance.status in ["ERR", "FIN"] or instance.percent == 100:
type = kwargs["type"]
notification = Notification()
notification.account = instance.owner
notification.process = instance
notification.status = "UNREAD"
notification.content = instance.description
notification.notification_type = type
if instance.sub_processes.exists():
for sub_process in instance.sub_processes.all():
if sub_process.model_id != "":
notification.model_id = sub_process.model_id
if sub_process.model_name != "":
notification.model_name = sub_process.model_name
notification.save()

这很好,但我的问题是,当我保存进程时,我需要传递一个额外的参数,这个参数是针对我创建的通知对象,即type,所以每次我保存进程对象(例如在视图中(时,我都试图做类似于process.save(type="EMM")的事情,但它不起作用,create_Notification信号中的行type = kwargs["type"]什么都没做,那么我如何发送额外的参数来保存方法呢?

也许这可以工作:

# Process model
def save(self, type=None, *args, **kwargs):
...

self._type = type

return super(Process, self).save(*args, **kwargs)

# Process post_save
def create_notification(sender, instance, *args, **kwargs):
if instance._type is None:
return
if instance.status in ["ERR", "FIN"] or instance.percent == 100:
type = instance._type
notification = Notification()
notification.account = instance.owner
notification.process = instance
notification.status = "UNREAD"
notification.content = instance.description
notification.notification_type = type
if instance.sub_processes.exists():
for sub_process in instance.sub_processes.all():
if sub_process.model_id != "":
notification.model_id = sub_process.model_id
if sub_process.model_name != "":
notification.model_name = sub_process.model_name
notification.save()

最新更新