[Fixed]-Django custom manager with ManyToManyField


From the docs on Using managers for related object access:

you can force Django to use the same class as the default manager for your model by setting the use_for_related_fields attribute on the manager class.

Meaning, in your case, you can force d.simulation to use the normal SimulationManager (and not DispatcherManager – DispatcherManager will be used for the opposite direction of the link. For example, Simulation.objects.get(id=1).dispatcher_set.completed).

I think the simplest way to achieve what you want is to define a get_completed_simulations and a get_queued_simulations methods in DispatcherManager. So the usage would be d.get_completed_simulations().

Leave a comment