Hello,

I've a complex SQL Query 
SELECT DISTINCT "event_event"."id", "event_event"."title", "event_event".
"description", "event_event"."category", "event_event"."event_time", 
"event_event"."event_place", "event_event"."time", "event_event"."cancelled"
, "event_event"."posted_by_id", 
CASE WHEN EXISTS (SELECT * FROM "event_eventviews" WHERE "event_eventviews".
"user_id" = 5 AND "event_eventviews"."event_id"=("event_event"."id")) THEN 
True ELSE False 
END AS "liked", 
CASE WHEN EXISTS (SELECT * FROM "event_eventviews" WHERE "event_eventviews".
"user_id" = 5 AND "event_eventviews"."event_id"=("event_event"."id")) THEN 
True ELSE False
 END AS "viewed" FROM "event_event" 
ORDER BY "event_event"."id" DESC

I want to convert it to Django Queryset, but I'm unable to do so. I tried 
annotation but that adds an outerjoin which results into extra result in 
queryset. 

Here is what I tried in Django: 

queryset = Event.objects.all().order_by('-id').annotate(
            viewed = Case(
                When(Q(views__user=request.user) & Q(views__event=F('id')), 
then=True),
                output_field=models.BooleanField(),
                default=False
            ),
            liked= Case(
                When(Q(likes__user=request.user) & Q(likes__event=F('id')), 
then=True),
                output_field=models.BooleanField(),
                default=False

            )
        )


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/74a0a64f-9ea6-469a-9acb-bd7fa22ccc7e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to