Django ORM 忽略 FilteredRelation 条件

huangapple go评论64阅读模式
英文:

Django ORM ignore FilteredRelation condition

问题

由于某种未知原因,FilteredRelation 中的条件似乎被忽略:

annotations["passport_index"] = FilteredRelation("countrypassportindex", condition=Q(passport_index__year=2020))
queryset = self.settings.base_model.objects.annotate(**annotations).values(*values_orm_keys)

这是 queryset.query 字段的值:

SELECT
    "countries_countrytotaltradevalue"."value",
    "countries_countrytotaltradevalue"."year",
    "countries_countrymilitaryexpenses"."value",
    "countries_countrymilitaryexpenses"."year",
    "countries_countryeconomicfreedomindex"."value",
    "countries_countryeconomicfreedomindex"."year",
    "countries_countrypayingtaxesindex"."value",
    "countries_countrypayingtaxesindex"."year",
    "countries_countrysuiciderate"."value",
    "countries_countrysuiciderate"."year",
    "countries_countrypassportindex"."value",
    "countries_countrypassportindex"."year"
FROM "countries_country"
LEFT OUTER JOIN "countries_countrytotaltradevalue"
    ON ("countries_country"."id" = "countries_countrytotaltradevalue"."country_id")
LEFT OUTER JOIN "countries_countrymilitaryexpenses"
    ON ("countries_country"."id" = "countries_countrymilitaryexpenses"."country_id")
LEFT OUTER JOIN "countries_countryeconomicfreedomindex"
    ON ("countries_country"."id" = "countries_countryeconomicfreedomindex"."country_id")
LEFT OUTER JOIN "countries_countrypayingtaxesindex"
    ON ("countries_country"."id" = "countries_countrypayingtaxesindex"."country_id")
LEFT OUTER JOIN "countries_countrysuiciderate"
    ON ("countries_country"."id" = "countries_countrysuiciderate"."country_id")
LEFT OUTER JOIN "countries_countrypassportindex"
    ON ("countries_country"."id" = "countries_countrypassportindex"."country_id")

你可以看到,在 LEFT OUTER JOIN ... ON ("countries_country"."id" = "countries_countrypassportindex"."country_id") 内部,没有 "countries_countrypassportindex"."year" = 2019

英文:

For some unknown reason condition in FilteredRelation seems to be ignored

annotations["passport_index"] = FilteredRelation("countrypassportindex", condition=Q(passport_index__year=2020))
queryset = self.settings.base_model.objects.annotate(**annotations).values(*values_orm_keys)

Here's queryset.query field value

SELECT
"countries_countrytotaltradevalue"."value",
"countries_countrytotaltradevalue"."year",
"countries_countrymilitaryexpenses"."value",
"countries_countrymilitaryexpenses"."year",
"countries_countryeconomicfreedomindex"."value",
"countries_countryeconomicfreedomindex"."year",
"countries_countrypayingtaxesindex"."value",
"countries_countrypayingtaxesindex"."year",
"countries_countrysuiciderate"."value",
"countries_countrysuiciderate"."year",
"countries_countrypassportindex"."value",
"countries_countrypassportindex"."year"
FROM "countries_country"
LEFT OUTER JOIN "countries_countrytotaltradevalue"
ON ("countries_country"."id" = "countries_countrytotaltradevalue"."country_id")
LEFT OUTER JOIN "countries_countrymilitaryexpenses"
ON ("countries_country"."id" = "countries_countrymilitaryexpenses"."country_id")
LEFT OUTER JOIN "countries_countryeconomicfreedomindex"
ON ("countries_country"."id" = "countries_countryeconomicfreedomindex"."country_id")
LEFT OUTER JOIN "countries_countrypayingtaxesindex"
ON ("countries_country"."id" = "countries_countrypayingtaxesindex"."country_id")
LEFT OUTER JOIN "countries_countrysuiciderate"
ON ("countries_country"."id" = "countries_countrysuiciderate"."country_id")
LEFT OUTER JOIN "countries_countrypassportindex"
ON ("countries_country"."id" = "countries_countrypassportindex"."country_id")

You can see that inside LEFT OUTER JOIN ... ON ("countries_country"."id" = "countries_countrypassportindex"."country_id") there is no "countries_countrypassportindex"."year" = 2019

答案1

得分: 0

"passport_index"应该在"values_orm_keys"中。

英文:

String passport_index should be in values_orm_keys

huangapple
  • 本文由 发表于 2023年7月7日 05:14:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定