Like user710907 says, Maintenance.taskRef.all() should do the trick. When you specify a ForeignKey relation, you can set a "related_name" argument that is used for the reverse relationship. If you don't specify, this defaults to "model_name_set".
class Foo(models.Model):
...
class Bar(models.Model):
...
foo = models.ForeignKey(Foo, related_name="related_bars")
Now you can access that relationship via bar.foo
from the one side, or foo.related_bars
from the other. If you don't specify, the latter argument will default to foo.bar_set
.
In your example, you define the relationship from both sides; the FK is defined on Task, and the reference is defined on Maintenance and Template. Since you've provided fields on both models that handle the relationship, there's no need to provide a maintenance_set default.
edit: Upon further inspection, I think part of the confusion might be around your Task.maintenance
field. If I'm not mistaken, that's the field you want to point to either Maintenance
or Template
, right? So why is it called maintenance
? I definitely recommend reading this section of the docs too.