Opened 8 years ago

Closed 8 years ago

#28328 closed Bug (invalid)

QuerySet.only doesn't work with QuerySet.difference

Reported by: Dmitry Dygalo Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
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

models.py:

class Model(models.Model):
    description = models.CharField(max_length=255)

Actual behavior:

>>> to_remove = Model.objects.filter(description='TO_REMOVE')
>>> query_set = Model.objects.all().difference(to_remove).only('pk')
>>> print(query_set.query)
(SELECT "test_model"."id", "test_model"."description" FROM "test_model") EXCEPT (SELECT "test_model"."id", "test_model"."description" FROM "test_model" WHERE "test_model"."description" = TO_REMOVE)

Expected:

>>> to_remove = Model.objects.filter(description='TO_REMOVE')
>>> query_set = Model.objects.all().difference(to_remove).only('pk')
>>> print(query_set.query)
(SELECT "test_model"."id" FROM "test_model") EXCEPT (SELECT "test_model"."id", "test_model"."description" FROM "test_model" WHERE "test_model"."description" = TO_REMOVE)

The problem:
Even if I use .only('pk') there is still "description" column in SELECT in the query, but I expect only "id" field as was specified in only.

Change History (1)

comment:1 by Tim Graham, 8 years ago

Resolution: invalid
Status: newclosed
Type: UncategorizedBug

I believe it's unsupported but doesn't raise an error yet. As per the documentation, "only LIMIT, OFFSET, and ORDER BY (i.e. slicing and order_by()) are allowed on the resulting QuerySet." See #27995 for a ticket to raise a helpful message about this.

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