【发布时间】:2023-02-11 14:03:14
【问题描述】:
我有一个Order和Notification模型,每当有新订单模型实例时,我想立即为新订单创建一个通知,我在模型中写了一些函数但是当有新订单时,通知实例不会被创建。我仍然认为应该有更好的方法来做到这一点,我该怎么做呢?
模型.py
class Orders(models.Model):
service = models.ForeignKey(Service, on_delete=models.SET_NULL, null=True, related_name="service_orders")
seller = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name="seller")
def create_notification(self):
create_notification = Notification.objects.create(provider=self.seller, service=self.service, order=self , notification_type='new_order')
create_notification.save()
return create_notification
class Notification(models.Model):
provider = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name="provider_notifications")
service = models.ForeignKey(Service, on_delete=models.SET_NULL, null=True, related_name="service_notifications")
order = models.ForeignKey(Orders, on_delete=models.SET_NULL, null=True, related_name="service_order")
notification_type = models.CharField(max_length=100, choices=NOTIFICATION_TYPE, default="none")
【问题讨论】:
-
你在哪里调用
create_notification函数?
标签: django django-models django-rest-framework