I'm having issues with queryset.intersection(...).filter(...)
. I found that the filter()
call does simply not do anything, but returns the same results as the queryset.intersection()
.
I now present an illustrative example. minex/models.py
file:
from uuid import uuid4
from django.db import models
class Channel(models.Model):
name = models.CharField(max_length=255)
class Release(models.Model):
version = models.CharField(max_length=255)
uuid = models.CharField(max_length=100, default="")
channels = models.ManyToManyField(to=Channel, related_name='releases')
def save(self, *args, **kwargs):
if not self.uuid:
self.uuid = str(uuid4())
super(Release, self).save(*args, **kwargs)
class Serial(models.Model):
name = models.CharField(max_length=255)
releases = models.ManyToManyField(to=Release, related_name='serials', blank=True)
Illustration of the problem: dbtest.py
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'minexample.settings')
from minexample.wsgi import application
from minex.models import Serial, Release, Channel
channel = Channel.objects.create(name="stable")
r1 = Release.objects.create(version="1.0")
r1.channels.add(channel)
r06 = Release.objects.create(version="0.6.2")
r06.channels.add(channel)
serial = Serial.objects.create(name="Some Serial")
serial.releases.add(r06)
releases = serial.releases.all().intersection(channel.releases.all())
assert r1 not in releases # works as expected
assert not releases.filter(uuid=r1.uuid).exists() # assertion failed... why?
Note: the last assertion does not fail if we instead did releases = serial.releases
. The problem seems to be that I'm calling intersection()
. I also stepped into Django's code and the WHERE statement does not contain the uuid=r1.uuid
at all... Is this a bug or am I doing something wrong?
Django now gives an error informing that filter()
is not supported after applying intersection()
:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.