Ich habe ein Modell mit einem ManyToManyField mit einem Through-Modell, in dem es ein boolesches Feld gibt, nach dem ich filtern möchte.Django Custom Manager mit 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()
Ich dachte, dass die use_for_related_fields
Variable mir erlauben würde, die M2M-Ergebnisse als auf einem Dispatcher d zu filtern, wie so: d.simulations.completed()
oder d.simulations.queued()
aber diese scheinen nicht, wie ich erwartet hatte, zu arbeiten. Missverständnis ich, wie die use_for_related_fields
funktioniert, oder mache ich etwas falsch?