我有一个模型与ManyToManyField与直通模型,其中有一个布尔字段,我想过滤。Django与ManyToManyField的自定义管理器
from simulations.models import *
class DispatcherManager(models.Manager):
use_for_related_fields = True
def completed(self):
original = super(DispatcherManager,self).get_query_set()
return original.filter(dispatchedsimulation__status=True)
def queued(self):
original = super(DispatcherManager,self).get_query_set()
return original.filter(dispatchedsimulation__status=False)
class Dispatcher(models.Model):
name = models.CharField(max_length=64)
simulations = models.ManyToManyField('simulations.Simulation',
through='DispatchedSimulation')
objects = DispatcherManager()
class DispatchedSimulation(models.Model):
dispatcher = models.ForeignKey('Dispatcher')
simulation = models.ForeignKey('simulations.Simulation')
status = models.BooleanField()
我认为use_for_related_fields
变量,让我来过滤M2M结果上像这样一个调度器d:d.simulations.completed()
或d.simulations.queued()
但这些不会出现,因为我早就预料到工作。我误解use_for_related_fields
是如何工作的,或者我做错了什么?