1

我正在尝试使用名为django-cart的 Django 电子商务插件进行编码。我想找到购物车中所有商品的价格总和。购物车模型具有以下代码:

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class Cart(models.Model):
    creation_date = models.DateTimeField(verbose_name=_('creation date'))
    checked_out = models.BooleanField(default=False, verbose_name=_('checked out'))
    def _valor_carrinho(self):
        return self.objects.all().aggregate(Sum('total_price'))
    valor_carrinho = property(_valor_carrinho)

    class Meta:
        verbose_name = _('cart')
        verbose_name_plural = _('carts')
        ordering = ('-creation_date',)

    def __unicode__(self):
        return unicode(self.creation_date)    

class ItemManager(models.Manager):
    def get(self, *args, **kwargs):
        if 'product' in kwargs:
            kwargs['content_type'] = ContentType.objects.get_for_model(type(kwargs['product']))
            kwargs['object_id'] = kwargs['product'].pk
            del(kwargs['product'])
        return super(ItemManager, self).get(*args, **kwargs)

class Item(models.Model):
    cart = models.ForeignKey(Cart, verbose_name=_('cart'))
    quantity = models.PositiveIntegerField(verbose_name=_('quantity'))
    unit_price = models.DecimalField(max_digits=18, decimal_places=2, verbose_name=_('unit price'))
    # product as generic relation
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    link_item = models.CharField(max_length=100)
    tipo_item = models.CharField(max_length=50)
    cor_item = models.CharField(max_length=50, blank=True)
    modelo_item = models.CharField(max_length=50, blank=True)
    tamanho_item = models.CharField(max_length=50, blank=True)

    objects = ItemManager()

    class Meta:
        verbose_name = _('item')
        verbose_name_plural = _('items')
        ordering = ('cart',)

    def __unicode__(self):
        return u'%d units of %s' % (self.quantity, self.product.__class__.__name__)

    def total_price(self):
        return self.quantity * self.unit_price
    total_price = property(total_price)

    # product
    def get_product(self):
        return self.content_type.get_object_for_this_type(id=self.object_id)

    def set_product(self, product):
        self.content_type = ContentType.objects.get_for_model(type(product))
        self.object_id = product.pk

    product = property(get_product, set_product)

而且我正在尝试使用valor_carrinho(抱歉代码中葡萄牙语和英语之间的混淆)作为total_price购物车中所有项目的字段总和。但是当我在模板中使用它时,它什么也不返回。我究竟做错了什么?

4

3 回答 3

2

我认为应该是:

def _valor_carrinho(self):
    return self.item_set.all().aggregate(Sum('total_price'))

要获取特定Cart实例的所有项目,我不确定total_price这里是否有效,但要访问购物车的项目,它是item_set,希望它足以让你开始!

使用的概念是反向外键查找,这里解释

于 2013-10-28T23:35:54.413 回答
0

我不确定这是否有效,因为我认为聚合进入数据库层并且 total_price 已被定义为属性。我将去规范化并使相当于 total_price 的字段,以便我可以使用聚合函数。

于 2014-06-19T21:16:16.740 回答
0

试试下面的代码,它对我有用,所以它可能对你有用:

视图.py

def get_cart(request):
    cart = Cart(request)
    cart.summary()
    return render_to_response('cart.html', dict(cart=Cart(request)),{'cart':cart})

购物车.html

<h3> Total : {{ cart.summary }}</h3>
于 2017-07-23T05:57:48.930 回答