Home > OS >  django: related_name of self related ForeignKey field not working | get opposite direction of self r
django: related_name of self related ForeignKey field not working | get opposite direction of self r

Time:12-02

Hej! :)

I have a model to create single institutions (Institution) and can connect it to a parent institution, via parent_institution (self).

So I have the institution A, which is parent to b, c, d (All themselves single institutions with an own detail view.) In the detail view of b I have the section 'parent institution' where I get A as a result, including a link to the detail view of A.

<p><b>Parent institution: </b>
    {% if institution.parent_institution %}
    <a href="{% url 'stakeholders:institution_detail' institution.parent_institution.id %}">
        {{institution.parent_institution}}
    </a>
    {% endif %}
</p>

Following this link I get to the detail view of A where I want a section with the child institutions. There should b, c, d be listed.

I added a related name to the parent_institution

class Institution(models.Model):
    name = models.CharField(
        verbose_name=_("Name of the institution"),
        max_length=200,
    )
    parent_institution = models.ForeignKey(
        "self",
        verbose_name=_("Parent institution"),
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
        help_text=_("if applicable"),
        related_name="child",
    )

normally I can follow the ForeignKey in the opposite direction via this related_name.

<p><b>Child institution: </b>
        {{institution.child.name}}
</p>

but in this case this is not working and gives me 'None'. Therefor did I try:

{% if institution.id == institution.all.parent_institution.id %}
      {{institution.name}}
{% endif %}
{% if institution.all.id == institution.parent_institution.id %}
     {{institution.name}}
{% endif %}
{% for child in institutions.all %}
{% if child.id == institution.parent_institution.id %}
{{institution.name}}
{% endif %}
{% endfor %}
# views.py

class InstitutionDetail(DetailView):
    model = Institution

    def get(self, request, *args, **kwargs):
        institutions_child = Institution.objects.filter(parent_institution__isnull=True).prefetch_related('parent_institution_set')
        institutions = get_object_or_404(Institution, pk=kwargs['pk'])

        context = {'institutions_child': institutions_child, 'institutions': institutions}

        return render(request, 'stakeholders/institution_detail.html', context)
{% for child_node in institutions.parent_institution_set.all %}
    {{child_node.name}}
{% endfor %}

I either get None or the current institutions name (A).

Does anyone know how I could achieve the goal of getting all the child institutions in the detail view?

Any help is appreciated! :)

CodePudding user response:

The Foreign Key in opposite direction returns a queryset, not a model instance.

<p><b>Child institution: </b></p>
<ul>
    {% for child in institutions.child.all %}
        <li>{{ child.name }}</li>
    {% endfor %}
</ul>

  • Related