在select_for_update中指定模型名称,而不是关系

时间:2019-10-14 22:57:03

标签: django postgresql

我想指定表/模型名称,而不是QuerySet模型中的关系。我链接到的关系是多表继承模型。

当我使用if (isEmptyPayload(payload)) { if (ann == null || ann.required()) { String paramName = getParameterName(parameter); BindingResult bindingResult = new BeanPropertyBindingResult(payload, paramName); bindingResult.addError(new ObjectError(paramName, "Payload value must not be empty")); throw new MethodArgumentNotValidException(message, parameter, bindingResult); } 并使用select_for_update参数提供关系时,仅“选择要更新的父表”。

考虑这段代码:

of

在这里, order = Order.objects.filter(uuid=order_uuid).prefetch_related( Prefetch( lookup='items', queryset=items ) ).select_related('outlet').select_related('user').select_related('outlet__group') \ .select_for_update(of=('self', 'outlet')) 是由 outlet

outlet = models.ForeignKey(Outlet, on_delete=models.PROTECT, related_name="orders")Order模型都是OutletTransaction模型(位于另一个Django应用程序中)的子模型

通过记录PSQL,这是正在执行的查询:

Cashless

(忽略SELECT "payment_gateway_transaction"."uuid", "payment_gateway_transaction"."ref_number", "payment_gateway_transaction"."shop_id", "payment_gateway_transaction"."user_id", "payment_gateway_transaction"."timestamp", "payment_gateway_transaction"."amount", "payment_gateway_transaction"."payment_method", "payment_gateway_transaction"."transaction_status", "payment_gateway_transaction"."service", "cashless_order"."transaction_ptr_id", "cashless_order"."token", "cashless_order"."outlet_id", "cashless_order"."order_status", "cashless_order"."order_start_time", "cashless_order"."order_end_time", "cashless_order"."order_collect_time", "cashless_order"."order_review", "cashless_order"."order_ratings", "authentication_user"."id", "authentication_user"."password", "authentication_user"."last_login", "authentication_user"."is_superuser", "authentication_user"."email", "authentication_user"."student_id", "authentication_user"."first_name", "authentication_user"."last_name", "authentication_user"."date_joined", "authentication_user"."ip_address", "authentication_user"."mac_address", "authentication_user"."location", "authentication_user"."phone_number", "authentication_user"."total_spent", "authentication_user"."is_banned", "authentication_user"."needs_student_id", "authentication_user"."generated_at", "authentication_user"."verify_token", "authentication_user"."is_active", "payment_gateway_shop"."uuid", "payment_gateway_shop"."name", "payment_gateway_shop"."location_id", "payment_gateway_shop"."location_coords", "payment_gateway_shop"."tax_applicable", "payment_gateway_shop"."percentage_tax", "payment_gateway_shop"."gst_number", "payment_gateway_shop"."open", "payment_gateway_shop"."active", "payment_gateway_shop"."group_id", "cashless_outlet"."shop_ptr_id", "cashless_outlet"."start_time", "cashless_outlet"."end_time", "cashless_outlet"."break_start_time", "cashless_outlet"."break_end_time", "cashless_outlet"."items_limit", "cashless_outlet"."image", "cashless_outlet"."description", "cashless_outlet"."on_cashless", "cashless_outlet"."ratings", "cashless_outlet"."latest_token", "auth_group"."id", "auth_group"."name" FROM "cashless_order" INNER JOIN "payment_gateway_transaction" ON ("cashless_order"."transaction_ptr_id" = "payment_gateway_transaction"."uuid") INNER JOIN "authentication_user" ON ("payment_gateway_transaction"."user_id" = "authentication_user"."id") INNER JOIN "cashless_outlet" ON ("cashless_order"."outlet_id" = "cashless_outlet"."shop_ptr_id") INNER JOIN "payment_gateway_shop" ON ("cashless_outlet"."shop_ptr_id" = "payment_gateway_shop"."uuid") INNER JOIN "auth_group" ON ("payment_gateway_shop"."group_id" = "auth_group"."id") WHERE "cashless_order"."transaction_ptr_id" = '6e413b2e- 8f07-4452-8cd2-5bc186e7ffcb'::uuid FOR UPDATE OF "payment_gateway_transaction", "payment_gateway_shop" 表以及UUID和预取。它们按预期方式工作。)

在查询中可以看到,authentication_userpayment_gateway_transaction是锁定用于更新的表。实际上,我需要子模型(我想要锁定的payment_gateway_shopOrder子模型)。

除了未指定Outlet并在Python中重构代码外,还有其他方法可以用来指定特定表吗?这是针对Django的吗?

感谢您的帮助。

0 个答案:

没有答案