python - Django items = order.orderitem_set.all() 返回数量的空值

标签 python python-3.x django django-models django-views

当我尝试放置 {{ item.product.quantity }} 时在它返回一个空值。数量值填入数据库。我似乎无法确定问题所在。我曾尝试打印数量值但得到相同的结果。
购物车.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' %}">&#x2190; 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 %}
View .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)
模型.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
如果我这样做 print(items), ]>

最佳答案

所以quantity是模型的一个字段 OrderItem .
您对返回的查询 items对象是:

order, created = Order.objects.get_or_create(customer=customer, complete=False)
Order存在:
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)
quantity
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)
所以你返回的对象不包含
{{ item.product.quantity }}
所以你必须使用(感谢您突出显示 @Jakumi )
{{ item.quantity }}

关于python - Django items = order.orderitem_set.all() 返回数量的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62728416/

相关文章:

python - pyzmq 发布者可以从类实例进行操作吗?

python - 当 'atomic' block 处于事件状态时,这是被禁止的。 Django 1.8

python - 提高将 Lab 颜色列表映射到第二个 Lab 颜色列表的速度

python - 列表中元组的流量强度天数

python - Django Rest Framework 外键嵌套

Django 新手 - NoReverseMatch 错误

python - 模拟修补类方法不起作用

python - chat() 缺少 1 个必需的位置参数 : 'id'

python - 根据匹配日期从另一个模型获取值(value)

python - 有没有办法在 plotly 中使用时间轴动画分组条形图?