2016-03-23 3 views
1

Я довольно новый в Django ... Мой вопрос: как я могу получить имя «self foreign key» в шаблоне через listView? Благодаря ...Показать ссылки в шаблоне

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

Что не работает? – Sayse

ответ

1

Ваше имя поля mother_organization, так что вы должны использовать то же имя поля в шаблоне:

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

Спасибо, но уже опробован (но не упоминал -> новое редактирование) и не работал ... извините – cedrik

+0

Что значит «не работает»? Какая у вас ошибка? –

+0

Шаблон возвращает значение по умолчанию ("-"). На странице администратора все в порядке! – cedrik

 Смежные вопросы

  • Нет связанных вопросов^_^