-1

我正在使用夹层和墨盒制作购物车。

在我的订单模型中,有一个名为 status 的字段,可以选择未处理和已处理。下订单后,管理员可以更改其状态。每当管理员更新状态时,我想向用户发送电子邮件。

我已经对其进行了一些研发,但我没有找到任何可以告诉我如何发送关于状态更改的电子邮件的信息。

我的模型:

class Order(models.Model):
    ...
    ....
    ........
    status = models.IntegerField(_("Status"),
                            choices=settings.SHOP_ORDER_STATUS_CHOICES,
                            default=settings.SHOP_ORDER_STATUS_CHOICES[0][0])

    objects = managers.OrderManager()

    # These are fields that are stored in the session. They're copied to
    # the order in setup() and removed from the session in complete().
    session_fields = ("shipping_type", "shipping_total", "discount_total")

    class Meta:
        verbose_name = _("Order")
        verbose_name_plural = _("Orders")
        ordering = ("-id",)

    def __unicode__(self):
        return "#%s %s %s" % (self.id, self.billing_name(), self.time)

    def billing_name(self):
        return "%s %s" % (self.billing_detail_first_name,
                          self.billing_detail_last_name)



    def setup(self, request):

        self.key = request.session.session_key
        self.user_id = request.user.id
        for field in self.session_fields:
            if field in request.session:
                setattr(self, field, request.session[field])
        self.total = self.item_total = request.cart.total_price()
        if self.shipping_total is not None:
            self.shipping_total = Decimal(str(self.shipping_total))
            self.total += self.shipping_total
        if self.discount_total is not None:
            self.total -= self.discount_total
        self.save()  # We need an ID before we can add related items.
        for item in request.cart:
            product_fields = [f.name for f in SelectedProduct._meta.fields]
            item = dict([(f, getattr(item, f)) for f in product_fields])
            self.items.create(**item)

    def complete(self, request):

        self.save()  # Save the transaction ID.
        for field in self.session_fields:
            if field in request.session:
                del request.session[field]
        del request.session["order"]
        for item in request.cart:
            try:
                variation = ProductVariation.objects.get(sku=item.sku)
            except ProductVariation.DoesNotExist:
                pass
            else:
                variation.update_stock(item.quantity * -1)
                variation.product.actions.purchased()
        code = request.session.get('discount_code')
        if code:
            DiscountCode.objects.active().filter(code=code).update(
                uses_remaining=F('uses_remaining') - 1)
        request.cart.delete()

    def details_as_dict(self):

        context = {}
        for fieldset in ("billing_detail", "shipping_detail"):
            fields = [(f.verbose_name, getattr(self, f.name)) for f in
                self._meta.fields if f.name.startswith(fieldset)]
            context["order_%s_fields" % fieldset] = fields
        return context

行政:

class OrderAdmin(admin.ModelAdmin):
    ordering = ("status", "-id")
    list_display = ("id", "billing_name", "total", "time", "status",
                    "transaction_id", "invoice")
    list_editable = ("status",)
    list_filter = ("status", "time")
    list_display_links = ("id", "billing_name",)
    search_fields = (["id", "status", "transaction_id"] +
                     billing_fields + shipping_fields)
    date_hierarchy = "time"
    radio_fields = {"status": admin.HORIZONTAL}
    inlines = (OrderItemInline,)
    formfield_overrides = {MoneyField: {"widget": MoneyWidget}}
    fieldsets = (
        (_("Billing details"), {"fields": (tuple(billing_fields),)}),
        (_("Shipping details"), {"fields": (tuple(shipping_fields),)}),
        (None, {"fields": ("additional_instructions", ("shipping_total",
            "shipping_type"), ("discount_total", "discount_code"),
            "item_total", ("total", "status"), "transaction_id")}),
    )
admin.site.register(Order, OrderAdmin)

谁能告诉我如何发送这封电子邮件?

4

1 回答 1

1

您需要在模型的post_save信号上添加一个侦听器。您的侦听器(这是一种标准的 Python 方法)将检查哪些字段已更改,然后将电子邮件发送给用户。

于 2013-01-09T12:30:55.347 回答