如何在第一个左外部联接上添加OR条件?如下所示:
LEFT OUTER JOIN "orders_orderedproductsku" ON ("manual_shipments"."id" = "orders_orderedproductsku"."shipment_id" OR "manual_shipments"."id" = "orders_orderedproductsku"."return_shipment_id")
我已经尝试了其他解决方法,但仍然没有希望。有没有可能我可以正确地添加OR条件?
下面是我的查询集:
shipment_rebooking = queryset \
.filter(status='BOOKING_CANCELED') \
.annotate(orderedproductsku__shipment_id=F('orderedproductsku__shipment_id'),
logistics_shipment__shipment_id=F('logisticsshipments__shipment_id'),
orderedproductsku__status=F('orderedproductsku__status'),
logistics_shipment__status=F('logisticsshipments__status'),
logistics_shipment__id=F('logisticsshipments__id')) \
.filter(Q(logistics_shipment__status='BOOKING_CANCELED') | Q(logistics_shipment__id__isnull=True),
orderedproductsku__status__in=['PREPARING_FOR_SHIPMENT', 'SCHEDULED_FOR_RETURN', 'SCHEDULED_FOR_RETURN_NO_REFUND']) \
.order_by('-created_at') \
.distinct()```
the raw query is like this:
```SELECT DISTINCT "manual_shipments"."created_at","manual_shipments"."updated_at","manual_shipments"."deleted_at","manual_shipments"."id","manual_shipments"."order_reference_number","manual_shipments"."logistics_provider_id","manual_shipments"."logistics_provider_shipment_id","manual_shipments"."logistics_name","manual_shipments"."tracking_number","manual_shipments"."tracking_url","manual_shipments"."shipping_cost","manual_shipments"."status","manual_shipments"."shipment_type","manual_shipments"."seller_id","manual_shipments"."buyer_id","manual_shipments"."latest_logistics_shipment_id","manual_shipments"."latest_status_id","orders_orderedproductsku"."shipment_id" AS "orderedproductsku__shipment_id","logistics_shipments"."shipment_id" AS "logistics_shipment__shipment_id","orders_orderedproductsku"."status" AS "orderedproductsku__status","logistics_shipments"."status" AS "logistics_shipment__status","logistics_shipments"."id" AS "logistics_shipment__id"出自"manual_shipments“
左外连接"orders_orderedproductsku“ON ("manual_shipments"."id”= "orders_orderedproductsku"."shipment_id")
左外连接"logistics_shipments“ON ("manual_shipments"."id”= "logistics_shipments"."shipment_id")
其中("manual_shipments"."status“= BOOKING_CANCELED
AND ("logistics_shipments"."status" = BOOKING_CANCELED OR "logistics_shipments"."id" IS NULL) AND "orders_orderedproductsku"."status" IN (PREPARING_FOR_SHIPMENT, SCHEDULED_FOR_RETURN, SCHEDULED_FOR_RETURN_NO_REFUND))按“manual_shipments”排序。“created_at”DESC`
发布于 2021-06-12 01:26:59
首先,左连接,右连接,离开这些东西。有了Django ORM,您就可以使用函数来执行数据库操作。Django有各种各样的函数可供使用。
要在django过滤器查询中执行OR操作,请执行以下操作:-
from django.db.models import Q
users = User.objects.filter(Q(email='sarath@gmail.com') | Q(first_name='john'))此查询的意思是-提供电子邮件为'sarath@gmail.com‘或名字为'john’的用户。
'|‘代表OR。
使用Q,你可以组合任意数量的过滤器。
https://stackoverflow.com/questions/67930464
复制相似问题