[Answered ]-Multiple reverse foreign key traversal

2👍

Your question doesn’t make sense as posed. As you know, each ShiftGroup has multiple shifts, each Shift has multiple runs, as so on. So there is no such thing as “the job_type” for a ShiftGroup: there are many.

If you’re asking for a list of all job_types in jobs in runs in shifts in the current ShiftGroup, then you should start with JobType, and use the double-underscore syntax:

JobType.objects.filter(job__run__shift__shift_group=self)

but again, this is a list of jobtypes, not a single one. If it is the case that all these relationships can only end in a case where there is one single jobtype, then you have something wrong with your data modelling: perhaps you should have a direct ForeignKey from ShiftGroup to JobType.

0👍

In your template, you might do something like this:

{% for shift in shiftgroups.shift_set.all %}
  {% for run in shift.run_set.all %}
    {% for job in run.job_set.all %}
      {{ job.job_type }}
    {% endfor %}
  {% endfor %}
{% endfor %}

This may be somewhat expensive though if you have lots of objects.

👤caram

Leave a comment