#27144: It is may be bug the Django ORM, may be I bad know SQL may it badly read
the Django docs and made googling.
-------------------------------+--------------------
Reporter: setivolkylany | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
Dublication objects, if using annotation with models.F() (not the
models.Count - it is working properly) on ManyToManyField
Code:
{{{
print('Real count books =\t', Book.objects.count())
print('Total count tags in all books =\t',
Book.objects.annotate(
count_tags=models.Count('tags', distinct=True)
).aggregate(
total_used_count_tags=models.Sum('count_tags')
)['total_used_count_tags']
)
qs =
Book.objects.values('tags').annotate(tags_names=models.F('tags__name'))
print('Query:\n', qs.query)
print('Count books after query =\t', qs.count())
print('Raw query return next count books =\t', len([i for i in
Book.objects.raw("""
SELECT
"books"."id",
"books_tags"."tag_id",
"tags"."name" AS "tags_names"
FROM "books"
LEFT OUTER JOIN
"books_tags" ON ("books"."id" = "books_tags"."book_id")
LEFT OUTER JOIN
"tags" ON ("books_tags"."tag_id" = "tags"."id")
ORDER BY
"books"."date_added" ASC
"""
)]))
}}}
Output
Real count books = 5
Total count tags in all books = 13
Query: SELECT "books_tags"."tag_id", "tags"."name" AS "tags_names"
FROM "books" LEFT OUTER JOIN "books_tags" ON ("books"."id" =
"books_tags"."book_id") LEFT OUTER JOIN "tags" ON ("books_tags"."tag_id" =
"tags"."id") ORDER BY "books"."date_added" ASC
Count books after query = 13
Raw query return next count books = 13
Models:
{{{
class Book(models.Model):
"""
Model for books
"""
id = models.UUIDField(primary_key=True, editable=False,
default=uuid.uuid4)
#other fields
tags = models.ManyToManyField(
Tag,
verbose_name=_('Tags'),
related_name='books',
)
class Tag(models.Model):
"""
Model tags for another models
"""
name = models.SlugField(
_('name'),
max_length=30,
unique=True,
allow_unicode=True,
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27144>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/056.070b825c6d4cbfc03fa1204275f9d926%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.