9👍
I have done something like that:
qs = queryset \
.annotate(
field_a=ArrayAgg(Case(When(
related_model__field="A",
then="related_model__pk")
)),
field_b=ArrayAgg(Case(When(
related_model__field="B",
then="related_model__pk")
)),
field_c=ArrayAgg(Case(When(
related_model__field="C",
then="related_model__pk")
))
)
Now there are lists of None
or pk
under each field_a
, field_b
and field_c
for every object in queryset. You can also define other default value for Case
instead of None
.
👤zyks
- [Django]-PermissionError: [Errno 13] Permission denied: '/manage.py'
- [Django]-Is Django post_save signal asynchronous?
- [Django]-Accessing dictionary by key in Django template
Source:stackexchange.com