我有一個模型與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
是如何工作的,或者我做錯了什麼?