我遇到queryset.intersection(...).filter(...)
的问题。我发现filter()
调用根本不执行任何操作,但返回的结果与queryset.intersection()
相同。
我现在提供一个说明性示例。 minex/models.py
个文件:
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)
问题的说明: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?
注意:如果我们改为releases = serial.releases
,则最后一个断言不会失败。问题似乎是我在打电话给intersection()
。我还进入了Django的代码,WHERE语句根本不包含uuid=r1.uuid
...这是一个错误还是我做错了什么?
答案 0 :(得分:0)
在应用filter()
之后,Django现在给出错误消息,通知不支持intersection()
: