Opened 5 years ago
Closed 5 years ago
#31549 closed Bug (invalid)
Wrong results when executing QuerySet.query.
Reported by: | Lawes | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 2.0 |
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 (last modified by )
We have a class:
from django.db.models import models class MyModel(models.Model): import_at = models.DateField( db_index=True, null=True, default=None,)
then I can get some results by:
queryset = MyModel.objects.filter(import_at__month=5, import_at__year=import_at[0]) print(queryset.count())
It will print 4
but when I used connection, I got diffrent result:
from django.db import transaction, connection with connection.cursor() as cursor: cursor.execute(queryset.query) print(len(cursor.fetchall()))
It will print 0
I got 0 !
If I using month or year, and get result by cursor.execute(queryset.query), we have diffrent result from connection.
Change History (2)
comment:1 by , 5 years ago
Description: | modified (diff) |
---|
comment:2 by , 5 years ago
Description: | modified (diff) |
---|---|
Resolution: | → invalid |
Status: | new → closed |
Summary: | Diffrent result from connection → Wrong results when executing QuerySet.query. |
Note:
See TracTickets
for help on using tickets.
queryset.query
is a helper, it doesn't produce SQL that can be executed directly in a database console, see #25705.