Hi Shai,

thanks! I need to look a bit deeper into this I guess.

Just for completeness, I am looking at this code in one of my projects:

```
def get_all_colors(self, queryset):
color_ids = queryset.order_by('color').values_list('color', flat=True).distinct() found_colors = Color.objects.filter(id__in=color_ids).prefetch_related('translations') result = [c.safe_translation_getter("name", any_language=True) for c in found_colors]
```

color is a ForeignKey. safe_translation_getter() is from django-parler and needs an actual model instance.

I was wondering if I can get rid of the additional Color.objects.filter() query.

But I guess this is not such a common use-case and belongs on django-users anyway.

Thanks for the clarification!

Regards,
René


On 21.07.20 20:52, Shai Berger wrote:
Hi René,

On Tue, 21 Jul 2020 18:46:12 +0200
René Fleschenberg <r...@fleschenberg.net> wrote:

https://github.com/kako-nawao/django-group-by


It doesn't actually aggregate, so the name "group-by" seems
unwarranted. What it does, as the README explains, is replace "values"
by something which does two things:

- Sets the seclected fields as attributes on the object returned,
   rather than as dict values
- Where the selected field is a FK, follows the relationship to get the
   object

Is there a deeper reason why this functionality is not available in
Django core?

If I'm not mistaken, only() provides this functionality, with the
difference that if you then try to access an unselected attribute on the
object, with only() it is fetched, while with this "group_by()" you get
an AttributeError.

(django-group-by has other limitations -- e.g. it is designed to
handle "rel__field" but not "rel1__rel2__field")

Did I miss anything important?


--
You received this message because you are subscribed to the Google Groups "Django 
developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/c4040127-573a-57cb-7005-59a7bbf87d84%40fleschenberg.net.

Reply via email to