Reputation: 1673
I have the following view where I want to return the quantity of the current cart entry.
def test_view(request):
cart_obj, new_obj = Cart.objects.new_or_get(request)
my_carts_current_entries = Entry.objects.filter(cart=cart_obj)
product_quantity = request.POST.get('product_quantity')
return render(request, 'carts/test.html', {'my_cart': cart_obj, 'my_carts_current_entries': my_carts_current_entries})
How would I reference the current entry quantity, e.g. if there is an entry in the database called 23x Chicken Nuggets I want it to return the quantity.
On the template if I return:
{{ my_carts_current_entries }}
it will return all the current entries but without the quantity.
For clarity I have included an extract of my models.py from the said application:
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
count = models.PositiveIntegerField(default=0)
total = models.DecimalField(default=0.00, max_digits=10, decimal_places=2)
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)
objects = CartManager()
def __str__(self):
return "Cart:{} User:{} Items:{} Total:£{}".format(self.id, self.user, self.count, self.total)
class Entry(models.Model):
product = models.ForeignKey(Product, null=True)
cart = models.ForeignKey(Cart, null=True)
quantity = models.PositiveIntegerField(default=0)
def __str__(self):
return self.product.name
Upvotes: 1
Views: 104
Reputation: 1660
try this in template:
{% for cart in my_carts_current_entries %}
{{ cart.product }} - {{ cart.quantity }}
{% endfor %}
Upvotes: 1