Opened 16 years ago

Closed 16 years ago

Last modified 12 years ago

#8283 closed (fixed)

.filter() is ignored after (query | query) construction

Reported by: Mikhail Gusarov Owned by: Malcolm Tredinnick
Component: Database layer (models, ORM) Version: dev
Severity: Keywords:
Cc: Alexander Solovyov, Benjamin Schwarze Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Model:

from django.db import models
from django.contrib.auth.models import User

class Asset(models.Model):
    public = models.BooleanField(default=True)
    owner = models.ForeignKey(User)

    def __unicode__(self):
        return "%s, %d (%s)" % (self.owner.username, self.id, "public" if self.public else "private")

Let's create several assets:

>>> from testqsbug.models import Asset
>>> from django.contrib.auth.models import User
>>> user1 = User.objects.get(username = "user1")
>>> user2 = User.objects.get(username = "user2")
>>> Asset.objects.create(public=True, owner=user1)
<Asset: user1, 1 (public)>
>>> Asset.objects.create(public=False, owner=user1)
<Asset: user1, 2 (private)>
>>> Asset.objects.create(public=True, owner=user2)
<Asset: user2, 3 (public)>
>>> Asset.objects.create(public=False, owner=user2)
<Asset: user2, 4 (private)>

Let's query all public assets OR assets that belong to some user:

>>> Asset.objects.filter(public=True) | Asset.objects.filter(owner=user1)
[<Asset: user1, 1 (public)>, <Asset: user1, 2 (private)>, <Asset: user2, 3 (public)>]

Let's filter from the result assets owned by user again (silly, I know):

>>> (Asset.objects.filter(public=True) | Asset.objects.filter(owner=user1)).filter(owner=user1)
[<Asset: user1, 1 (public)>, <Asset: user1, 2 (private)>, <Asset: user2, 3 (public)>]

Uhm. Does not work. Both requests resulted in the same query:

>>> q = (Asset.objects.filter(public=True) | Asset.objects.filter(owner=user1))
>>> print q.query
SELECT "testqsbug_asset"."id", "testqsbug_asset"."public", "testqsbug_asset"."owner_id" FROM "testqsbug_asset" WHERE ("testqsbug_asset"."public" = True  OR "testqsbug_asset"."owner_id" = 1 )
>>> q2 = (Asset.objects.filter(public=True) | Asset.objects.filter(owner=user1)).filter(owner=user1)
>>> print q2.query
SELECT "testqsbug_asset"."id", "testqsbug_asset"."public", "testqsbug_asset"."owner_id" FROM "testqsbug_asset" WHERE ("testqsbug_asset"."public" = True  OR "testqsbug_asset"."owner_id" = 1 )

Filtering by another user works fine:

>>> (Asset.objects.filter(public=True) | Asset.objects.filter(owner=user1)).filter(owner=user2)
[<Asset: user2, 3 (public)>]
>>> q3 = (Asset.objects.filter(public=True) | Asset.objects.filter(owner=user1)).filter(owner=user2)
>>> print q3.query
SELECT "testqsbug_asset"."id", "testqsbug_asset"."public", "testqsbug_asset"."owner_id" FROM "testqsbug_asset" WHERE (("testqsbug_asset"."public" = True  OR "testqsbug_asset"."owner_id" = 1 ) AND "testqsbug_asset"."owner_id" = 2 )

Change History (4)

comment:1 by Malcolm Tredinnick, 16 years ago

milestone: 1.0
Owner: changed from nobody to Malcolm Tredinnick
Triage Stage: UnreviewedAccepted

comment:2 by Benjamin Schwarze, 16 years ago

Cc: Benjamin Schwarze added

comment:3 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: newclosed

(In [8413]) Fixed #8283 -- Fixed an edge case when adding things to the "where" tree and
combining different connector types.

comment:4 by Jacob, 12 years ago

milestone: 1.0

Milestone 1.0 deleted

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