Last active
March 20, 2020 07:27
-
-
Save Ingco/da480e8e3fa062d6a7b6800dfc5e10b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Запрос: | |
company_subquery = ( | |
Order.objects_clear.annotate( | |
company_pk=F("client__company_id"), | |
company=Coalesce( | |
F("client__company__alias"), F("client__company__name") | |
), | |
) | |
) | |
company_name_subquery = company_subquery.values("company")[:1] | |
company_pk_subquery = company_subquery.values("company_pk")[:1] | |
received_companies = ( | |
Invoice.objects | |
.annotate( | |
received_company_pk=Subquery( | |
company_pk_subquery, output_field=IntegerField() | |
), | |
received_company=Subquery( | |
company_name_subquery, output_field=CharField() | |
) | |
) | |
# .order_by("id", "received_company_pk") | |
# .distinct("received_company_pk") | |
.values_list("received_company_pk", "received_company") | |
) | |
# Результат: | |
# <QuerySet [(4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), (4, 'MDK'), '...(remaining elements truncated)...']> | |
# Если раскомментировать `.distinct("received_company_pk")`, то получаю | |
NotImplementedError: annotate() + distinct(fields) is not implemented. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment