﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33382	Different count and len result for a distinct QuerySet	crazy	nobody	"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
}}}


"	Uncategorized	closed	Database layer (models, ORM)	3.2	Normal	duplicate			Unreviewed	0	0	0	0	0	0
