插入新记录时如何向用户发送短信



我有模型订单

class Order(models.Model):
STATUS = (
('Pending', 'Pending'),
('Out for delivery', 'Out for delivery'),
('Delivered', 'Delivered'),
)
shop = models.ForeignKey(Shop, models.CASCADE, null=True)
customer = models.ForeignKey(Customer, models.CASCADE, null=True)
product = models.ForeignKey(Product, models.CASCADE, null=True)
quantity = models.CharField(max_length=30, null=True, )
date_created = models.DateTimeField(auto_now_add=True, null=True)
status = models.CharField(max_length=200, null=True, choices=STATUS, default='Pending')
note = models.CharField(max_length=1000, null=True)

当客户插入订单时,我如何向商店发送短信,然后商店收到短信,您有新订单,customer.name 数量 product.name

客户模型

class Customer(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
name = models.CharField(max_length=100, null=True, default='customer')
Phone = models.CharField(null=True, max_length=10)

店铺模型

class Shop(models.Model):
user = models.OneToOneField(User, null=True, related_name='shop', blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=70, null=True, default='shop', )
address = models.CharField(max_length=200, null=True)
Phone = models.CharField(null=True, max_length=10)

和我的订单views.py

def CreateOrder(request, shop_id, product_id):
customer = request.user.customer
shop = Shop.objects.get(id=shop_id)
product = Product.objects.get(id=product_id)
form = OrderForm()
quantity = request.POST.get('quantity')
note = request.POST.get('note')
if request.method == 'POST':
order = Order.objects.create(customer=customer, shop=shop,
product=product, quantity=quantity, note=note)
return redirect('CustomerOrder')

你可以在 Django 中使用信号。基本上,当发生某些事件时,您的应用程序将收到通知。在您的情况下,您可以使用post_save,信号将在调用模型的save()方法后触发。在 Django 文档中,您可以查看可用不同类型的signals的完整列表。

from .models import Order, Shop, Customer
from django.dispatch import Signal, receiver
from django.db.models.signals import post_save

@receiver(post_save, sender=Order)
def send_msg_to_shop_owners(sender, instance, **kwargs):
# YOUR CODE HERE 
pass

receiver中,您可以使用请求将 API 调用发送到要使用的任何消息服务。

注意:每当调用Order型号的方法时post_save信号都会triggeredsave()。这将包括updating您的模型。因此,根据您的用例,您甚至可以 创建自己的custom signals.

Django 中的自定义信号

from django.dispatch import Signal, receiver
# DEFINE SIGNAL NAME WITH REQUIRED ARGUMENTS; FOR NOW JUST order_id, user_name
send_message_to_shop_owner = Signal(providing_args=["order_id","user_name"])
@receiver(send_message_to_shop_owner)
def send_message_to_shop_owner_receiver(sender, **kwargs):
# access the parameters passed
# order_id = kwargs["order_id"]
# user_name = kwargs["user_name"]
# REST OF YOUR CODE GOES HERE
pass

现在您已经定义了signal您需要在想要send消息时触发它。在drf我通常把它放在serializers.py里面,但你可以在你的views里做。 以下是您将如何触发信号。

views.py

from .signals import send_message_to_shop_owner
def CreateOrder(request, shop_id, product_id):
customer = request.user.customer
shop = Shop.objects.get(id=shop_id)
product = Product.objects.get(id=product_id)
form = OrderForm()
quantity = request.POST.get('quantity')
note = request.POST.get('note')
if request.method == 'POST':
order = Order.objects.create(customer=customer, shop=shop,
product=product, quantity=quantity, note=note)
# Ignore my custom parameters, instead of that you can send yours
send_message_to_shop_owner.send(Sender=Order, order_id=order_id,user_name=user_name)
return redirect('CustomerOrder')

注意:对于custom signals,您需要在要使用它时触发它。

相关内容

  • 没有找到相关文章

最新更新