2๐
โ
I think it should be:
def _valor_carrinho(self):
return self.item_set.all().aggregate(Sum('total_price'))
to get all items for a specific Cart
instance, I am NOT sure total_price
works here, but to access the items of the cart it is item_set
, hopefully it is enough to get you started!
The concept that that is using is reverse foreign key lookup, which is explained here
๐คdm03514
0๐
I am not sure that this does work as I think aggregate goes to the database layer and total_price has been defined as a property. I am going to denormalise and make the equivalent of total_price a field so that I can use the aggregate function.
๐คhum3
- [Answered ]-Django โ custom filter not working properly
- [Answered ]-Django โ how to restrict number of pages an unauthenticated user can view
- [Answered ]-Can I have an instance variable? (Answer at bottom of my post)
- [Answered ]-Django url pattern issue in python 2.7.6
0๐
Try the following code, it works for me so it might work for you:
view.py
def get_cart(request):
cart = Cart(request)
cart.summary()
return render_to_response('cart.html', dict(cart=Cart(request)),{'cart':cart})
cart.html
<h3> Total : {{ cart.summary }}</h3>
๐คPratik
- [Answered ]-UniqueValidator with source field
- [Answered ]-Django: CSS and Images (Static Files) loaded successfully but not applied
- [Answered ]-Migrate data from original User model to Custom User model in django 1.8
- [Answered ]-RemovedInDjango110Warning: The context_instance argument of render_to_string is deprecated
Source:stackexchange.com