gpt4 book ai didi

python - 如何排除Django中的数据

转载 作者:行者123 更新时间:2023-12-04 07:35:31 27 4
gpt4 key购买 nike

我试图排除在我的 orderRequest 表中的订单,但它只显示在 orderRequest 表中的订单并排除其他订单。Function 获得正确的订单 ID,但不排除该订单。它排除其他订单并显示已经在 orderRequest 表中的订单
查看.py

class OrderProduct_View(TemplateView):

template_name = 'purchase/orderProduct.html'

def get(self, request, *args, **kwargs):
allOrder = OrderProduct.objects.all()
allOrd = Order.objects.all()
categories = Category.objects.all()
categoryId = self.request.GET.get('SelectCategory')
product = Product.objects.filter(category_id=categoryId)

def filter_order(order):
try:
orderReq = order.orderRequest
return orderReq
except:
return True

filteredOrders = list(filter(filter_order, allOrd))

args = {'categories': categories, 'product': product, 'filteredOrders': filteredOrders}

return render(request, self.template_name, args)
模型.py
class OrderProduct(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
description = models.CharField(max_length=90)
quantity = models.IntegerField()
order_cancel = models.BooleanField(default=False)

class Order(models.Model):
orderProduct = models.ForeignKey(OrderProduct, on_delete=models.CASCADE)
created_by = models.CharField(max_length=20)
created_at = models.DateTimeField(auto_now_add=True)
destination = models.CharField(max_length=30)
vendor = models.ForeignKey(Vendor, on_delete=models.CASCADE)
shipping_cost = models.IntegerField()


class OrderRequest(models.Model):
order_status = models.CharField(max_length=10)
order = models.OneToOneField(Order, related_name='orderRequest', on_delete=models.CASCADE)
HTML
  {% for allOrders in filteredOrders%}
{{ allOrders.orderProduct.id }}
{{ allOrders.orderProduct.product.name }}
{{ allOrders.orderProduct.quantity }}
<button><a href="{% url 'allVendor' %}?product_id={{ allOrders.orderProduct.product.id }}&orderProducts_id={{ allOrders.orderProduct.id }}">Place Order</a></button>
{% endfor %}

最佳答案

您可以使用 isnull lookup [Django docs]查找没有相关实例的实例,因此要查找 Order没有 OrderRequest 的实例您可以使用的实例:

Order.objects.filter(orderRequest__isnull=True)
因此,您可以将 View 写为:
class OrderProduct_View(TemplateView):
template_name = 'purchase/orderProduct.html'

def get(self, request, *args, **kwargs):
filteredOrders = Order.objects.filter(orderRequest__isnull=True)
categories = Category.objects.all()
categoryId = self.request.GET.get('SelectCategory')
product = Product.objects.filter(category_id=categoryId)
args = {'categories': categories, 'product': product, 'filteredOrders': filteredOrders}
return render(request, self.template_name, args)

Note: Class names should be in PascalCase so OrderProductView instead of OrderProduct_View. Variable names should be insnake_case so filtered_orders instead of filteredOrders,etc. See PEP 8 -- Style Guide for PythonCode

关于python - 如何排除Django中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67764071/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com