From 64185e6890f4310a01755671a4d17238ec7ae6d2 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 28 May 2014 18:26:53 +0200 Subject: [PATCH] Add method for attach stars count to projects queryset. --- taiga/projects/stars/services.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/taiga/projects/stars/services.py b/taiga/projects/stars/services.py index 937ac526..98e335cc 100644 --- a/taiga/projects/stars/services.py +++ b/taiga/projects/stars/services.py @@ -80,3 +80,21 @@ def get_starred(user_or_id): qs = qs.filter(fans__user=user_or_id) return qs + + +def attach_startscount_to_queryset(queryset): + """ + Attach stars count to each object of projects queryset. + + Because of lazynes of starts objects creation, this makes + much simple and more efficient way to access to project + starts number. + + (The other way was be do it on serializer with some try/except + blocks and additional queryes) + """ + sql = ("SELECT coalesce(stars_stars.count, 0) FROM stars_stars " + "WHERE stars_stars.project_id = projects_project.id ") + qs = queryset.extra(select={"starts_count": sql}) + return qs +