2016-03-23 8 views
1

Ich bin ziemlich neu in Django ... Meine Frage: Wie kann ich den "self-Fremdschlüssel" Name in einer Vorlage über eine ListView erhalten? Dank ...Anzeigen von Selbstreferenzen in Vorlage

models.py

class Organization(Model.models): 
    name = models.CharField(max_length=100) 
    mother_organization = models.ForeignKey('self', 
              null=True, 
              blank=True, 
              related_name='mother' 
              ) 

views.py

class List(ListView): 
    model = Organization 
    context_object_name = "organizations" 
    template_name = "organizations.html" 

organizations.html

{% for organization in organizations %} 
    <p> 
     {{ organization.reference }} 
     {{ organization.name }} 
     test 0 : OK !! 
     {{ organization.mother_organization.name|default:"-" }} 
     test 1 : FAIL 
     {{ organization.mother.name|default:"-" }} 
     test 2 : FAIL 
     {% for mother in organization.mother.all %} 
      {{ mother.name }} 
     {% empty %} 
     test 3 : FAIL 
     {% for mother in organization.mother_organization.all %} 
      {{ mother.name }} 
     {% empty %} 

      # 
     {% endfor %} 
    </p> 
{% endfor %} 
+0

Was nicht funktioniert? – Sayse

Antwort

1

Ihr Feldname ist mother_organization, so sollten Sie den gleichen Feldnamen in der Vorlage verwenden:

{{ organization.mother_organization.name|default:"-" }} 
+0

Danke, aber schon getestet (aber nicht erwähnt -> neue Bearbeitung) und hat nicht funktioniert ... sorry – cedrik

+0

Was meinst du mit "hat nicht funktioniert"? Welchen Fehler hast du? –

+0

Die Vorlage gibt den Standardwert ("-") zurück. In der Admin-Seite ist alles in Ordnung! – cedrik

Verwandte Themen