#8389 closed (duplicate)
Q expression with & and | inconsistent
| Reported by: | Owned by: | Malcolm Tredinnick | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.0-alpha |
| Severity: | Keywords: | ||
| Cc: | bronger@… | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I use Q expressions in my DB query. It doesn't find anything although it should.
Consider the following models.py:
from django.db import models
from django.db.models import Q
import django.contrib.auth.models
from django.contrib import admin
class Sample(models.Model):
pass
class SampleSeries(models.Model):
originator = models.ForeignKey(django.contrib.auth.models.User)
samples = models.ManyToManyField(Sample, blank=True)
class UserDetails(models.Model):
user = models.OneToOneField(django.contrib.auth.models.User)
my_samples = models.ManyToManyField(Sample, blank=True)
import sys
if sys.argv[1] == "runserver":
try:
user = django.contrib.auth.models.User.objects.get(username="bronger")
except django.contrib.auth.models.User.DoesNotExist:
user = django.contrib.auth.models.User(username="bronger", password="12345")
user.save()
try:
user_details = UserDetails.objects.get(user=user)
except UserDetails.DoesNotExist:
user_details = UserDetails(user=user)
user_details.save()
if not SampleSeries.objects.count():
SampleSeries(originator=user).save()
print SampleSeries.objects.filter(
Q(samples__userdetails=user_details) | (Q(originator=user_details.user) & Q(originator=user_details.user)))
It will print
[]
However, if you delete on of the (obviously redundant) "Q(originator=user_details.user)", it does find the one SampleSeries:
[<SampleSeries: SampleSeries object>]
Change History (4)
comment:1 by , 17 years ago
| milestone: | → 1.0 |
|---|
comment:2 by , 17 years ago
| Owner: | changed from to |
|---|
comment:3 by , 17 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
Looks like this and #8439 are the same problem. I'll close this one in favour of the other, since I find the other example easier to follow.