queryset.values() / GROUP BY

69 views
Skip to first unread message

René Fleschenberg

unread,
Jul 21, 2020, 12:46:32 PM7/21/20
to django-d...@googlegroups.com
Hi,

on IRC, someone hinted me at
https://github.com/kako-nawao/django-group-by today.

Is there a deeper reason why this functionality is not available in
Django core, or is it just that nobody got around to implementing it
yet? To me, it seems like something that would be nice to have without
having to rely on a third-party package.

Any thoughts?

Regards,
René

Shai Berger

unread,
Jul 21, 2020, 2:52:47 PM7/21/20
to django-d...@googlegroups.com
Hi René,

On Tue, 21 Jul 2020 18:46:12 +0200
René Fleschenberg <re...@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?

Javier Buzzi

unread,
Jul 21, 2020, 2:53:40 PM7/21/20
to django-d...@googlegroups.com
This looks like a cool little side project. I would keep it there, the use case is very limited. I've always seen the django ORM as a very basic
> --
> 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-develop...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/0cdf647e-295f-6948-abd0-696cb9859efe%40fleschenberg.net.

Adam Johnson

unread,
Jul 21, 2020, 3:12:22 PM7/21/20
to django-d...@googlegroups.com
I didn't look too deeply at the project, but after checking the readme and Shai's summary:

 - 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

...I think both of these features can be replicated with the ORM already. A QS like `Model.objects.annotate(x=F('fk__x')).values_list('x', named=True)` demonstrates both features. annotate() allows grabbing related FK objects' values as direct attributes, and passing named=True to values_list returns namedtuples.



--
Adam

René Fleschenberg

unread,
Jul 21, 2020, 3:22:16 PM7/21/20
to django-d...@googlegroups.com
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é
Reply all
Reply to author
Forward
0 new messages