Try to add distinct() to query set.

On Fri, Mar 20, 2009 at 10:06 PM, eli <eliasz.wont...@gmail.com> wrote:
>
> Hi,
>
> I have a problem with Django Orm annotate method..
>
> My models (application: app):
>
> class File(models.Model):
>  name = models.CharField(max_length=255)
>  file = MyFileField(upload_to=...)
>
> class Visit(models.Model):
>  file = models.ForeignKey(File)
>
> class Comment(models.Model):
>  file = models.ForeignKey(File)
>
> #---------
>
>>> e = 
>>> File.objects.annotate(comment_cnt=Count('comment')).order_by('comment_cnt')
>>> print e[0].comment_cnt
>>> 3 # ok
>
>>> e = File.objects.annotate(visit_cnt=Count('visit')).order_by('visit_cnt')
>>> print e[0].visit_cnt
>>> 9 # ok
>
>>> e = File.objects.annotate(comment_cnt=Count('comment'), 
>>> visit_cnt=Count('visit')).order_by('comment_cnt')
>>> print e[0].visit_cnt
>>> 27 #  <----- ?? (3*9?) WRONG!
>>> print e[0].comment_cnt
>>> 27 #  <----- ?? (3*9?) WRONG!
>
> SQL Query dump:
>
> (...), COUNT("app_visit"."id") AS "visit_cnt", COUNT
> ("app_comment"."id") AS "comment_cnt" FROM "app_file" LEFT OUTER JOIN
> "app_visit" ON ("app_file"."id" = "app_visit"."file_id") LEFT OUTER
> JOIN "app_comment" ON ("app_file"."id" = "app_comment"."file_id")
> GROUP BY (...)
>
> What is wrong? Why, when I'm using more than one Count() function in
> annotate, I've got multiply results?
>
> Thanks for help..
>
> regards.
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to