Django CASE in queryset.extra with COUNT
I'm trying to use the COUNT'ed value of a joined table in a CASE statement
to tell whether a user has added content or not.
def get_queryset(self):
q = super(UserIndex, self).get_queryset()
q = q.annotate(story_count=Count('story'))
q = q.extra(select={
'added_content': '''
CASE
WHEN "story_count" > 0 THEN 1
ELSE 0
END
'''})
return q
Which blows up at the database level misuse of aggregate: COUNT(). Is
something like this even possible without having to resort to an extra
column that would require keeping track of?
No comments:
Post a Comment