英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论