Skip to content
Advertisement

Django items = order.orderitem_set.all() returning empty value for quantity

When I try and put the {{ item.product.quantity }} in it returns an empty value. The quantity value is filled in in the database. I can’t seem t pinpoint the problem. I have tried printing the quantity value but got the same results.

cart.html

{% extends 'store/main.html' %}
{% load static %}
{% block content %}
     <div class="row">
        <div class="col-lg-12">
            <div class="box-element">
                <a  class="btn btn-outline-dark" href="{% url 'store' %}">← Continue Shopping</a>
                <br>
                <br>
                <table class="table">
                    <tr>
                        <th><h5>Items: <strong>3</strong></h5></th>
                        <th><h5>Total:<strong> $42</strong></h5></th>
                        <th>
                            <a  style="float:right; margin:5px;" class="btn btn-success" href="{% url 'checkout' %}">Checkout</a>
                        </th>
                    </tr>
                </table>
            </div>

            <br>

            <div class="box-element">
                <div class="cart-row">
                    <div style="flex:2"></div>
                    <div style="flex:2"><strong>Item</strong></div>
                    <div style="flex:1"><strong>Price</strong></div>
                    <div style="flex:1"><strong>Quantity</strong></div>
                    <div style="flex:1"><strong>Total</strong></div>
                </div>
                {% for item in items %}
                    <div class="cart-row">
                        <div style="flex:2"><img class="row-image" src="{{ item.product.imageURL }}"></div>
                        <div style="flex:2"><p>{{ item.product.name }}</p></div>
                        <div style="flex:1"><p>R{{ item.product.price|floatformat:2 }}</p></div>
                        <div style="flex:1">
                            <p class="quantity">{{ item.product.quantity }}</p>
                            <div class="quantity">
                                <img class="chg-quantity" src="{% static  'images/arrow-up.png' %}">

                                <img class="chg-quantity" src="{% static  'images/arrow-down.png' %}">
                            </div>
                        </div>
                        <div style="flex:1"><p>$32</p></div>
                    </div>
                {% endfor %}
            </div>
        </div>
    </div>
{% endblock content %}

views.py

from django.shortcuts import render
from .models import *

def store(request):
    search = request.GET.get('search')
    products = Product.objects.all()

    if search != '' and search is not None:
        products = products.filter(name__icontains=search)

    context= {'products': products}
    return render(request, 'store/store.html', context)


def cart(request):
    if request.user.is_authenticated:
        customer = request.user.customer
        order, created = Order.objects.get_or_create(customer=customer, complete=False)
        items = order.orderitem_set.all()
    else:
        items = []

    context= {'items': items}
    return render(request, 'store/cart.html', context)

def checkout(request):
    context= {}
    return render(request, 'store/checkout.html', context)

models.py

from django.db import models
from django.contrib.auth.models import User


class Customer(models.Model):
    user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
    name = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200)

    def __str__(self):
        return self.name


class Product(models.Model):
    name = models.CharField(max_length=200, null=True)
    price = models.FloatField()
    digital = models.BooleanField(default=False, null=True, blank=True)
    image = models.ImageField(null=True, blank=True)

    def __str__(self):
        return self.name

    @property
    def imageURL(self):
        try:
            url = self.image.url
        except:
            url = ''
        return url


class Order(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
    date_ordered = models.DateTimeField(auto_now_add=True)
    complete = models.BooleanField(default=False, null=True, blank=False)
    transaction_id = models.CharField(max_length=100, null=True)

    def __str__(self):
        return str(self.id)

class OrderItem(models.Model):
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
    quantity = models.IntegerField(default=0, null=True, blank=True)
    date_added = models.DateTimeField(auto_now_add=True)


class ShippingAddress(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
    address = models.CharField(max_length=200, null=False)
    city = models.CharField(max_length=200, null=False)
    state = models.CharField(max_length=200, null=False)
    zipcode = models.CharField(max_length=200, null=False)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.address

If I do print(items) the result would be:

<QuerySet [<OrderItem: OrderItem object (4)>, <OrderItem: OrderItem object (5)>]>

Advertisement

Answer

So quantity is a field of the Model OrderItem.

Your query for your returned items object is:

order, created = Order.objects.get_or_create(customer=customer, complete=False)

with Order being:

class Order(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
    date_ordered = models.DateTimeField(auto_now_add=True)
    complete = models.BooleanField(default=False, null=True, blank=False)
    transaction_id = models.CharField(max_length=100, null=True)

    def __str__(self):
        return str(self.id)

and quantity in

class OrderItem(models.Model):
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
    quantity = models.IntegerField(default=0, null=True, blank=True)
    date_added = models.DateTimeField(auto_now_add=True)

So your returned object doesn’t contain

{{ item.product.quantity }}

So you gotta use (Thanks for highlighting it @Jakumi)

{{ item.quantity }}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement