Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33382 closed Uncategorized (duplicate)

Different count and len result for a distinct QuerySet

Reported by: crazy Owned by: nobody
Component: Database layer (models, ORM) Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The problem arises when I use the len method on a distinct queryset. The object obtained by the len method seems to be not distinct , But the query did not change during this time.

I found what looks like the same Ticket ,But I'm not sure it's the same reason https://code.djangoproject.com/ticket/30655

Here is a simple example that shows the contrary.

Models:

class Exam(models.Model):
    paper = models.ForeignKey(ExamPaper, related_name='paper_exam', on_delete=models.CASCADE, blank=True, null=True)

class StudentPaper(models.Model):
    user = models.ForeignKey(User, related_name='user_exam_paper', on_delete=models.CASCADE)
    exam = models.ForeignKey(Exam, related_name='exam_student_paper', on_delete=models.CASCADE)

Shell Output:

>>> from ExamManage.models import*
>>> exam = Exam.objects.first()
>>> exam.exam_student_paper.filter(is_pass=True).values('user_id').count()
521
>>> support_pass_userids = exam.exam_student_paper.filter(is_pass=True).values('user_id').distinct()
>>> support_pass_userids.count()
484
>>> print(support_pass_userids.query)
SELECT DISTINCT `ExamManage_studentpaper`.`user_id`, `ExamManage_studentpaper`.`id` FROM `ExamManage_studentpaper` WHERE (`ExamManage_studentpaper`.`exam_id` = 5 AND `ExamManage_studentpaper`.`is_pass`) ORDER BY `ExamManage_studentpaper`.`id` DESC
>>> len(support_pass_userids)
521
>>> support_pass_userids.count()
521
>>> print(support_pass_userids.query)
SELECT DISTINCT `ExamManage_studentpaper`.`user_id`, `ExamManage_studentpaper`.`id` FROM `ExamManage_studentpaper` WHERE (`ExamManage_studentpaper`.`exam_id` = 5 AND `ExamManage_studentpaper`.`is_pass`) ORDER BY `ExamManage_studentpaper`.`id` DESC

Change History (2)

comment:1 by Mariusz Felisiak, 2 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: duplicate
Status: newclosed

Duplicate of #30655. Please see my comment:

"count() calls SELECT COUNT(*) (as described in docs) without taking ordering into account,..."

see also a note about using distinct() on an ordered queryset.

comment:2 by crazy, 2 years ago

Thanks for your patience, now I understand why there difference, because the id field is used for sorting, it interfered with the distinct results. So if count records, should use count.

Note: See TracTickets for help on using tickets.
Back to Top