I have a model with a ManyToManyField with a through model in which there is a boolean field that I would like to filter upon.
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()
I thought that the use_for_related_fields
variable would allow me to filter the m2m results as on a dispatcher d like so: d.simulations.completed()
or d.simulations.queued()
but these do not appear to work as I had expected. Am I misunderstanding how the use_for_related_fields
works, or am I doing something wrong?