Django在模板中循环

时间:2019-05-10 13:04:36

标签: django django-models django-templates django-admin

我有两个关系为1-N的模型。我想编辑change_from_objects_tools.html来执行一个循环,以便在我拥有aditamento时。objecs正在创建按钮。

项目模型

class Projeto(models.Model):
   field1 = ........ 
   field2 = ........

Aditamento模型

class Aditamento(models.Model):

  processo = models.ForeignKey(Projet, on_delete=models.CASCADE,verbose_name="Projecto",related_name='ProcessoObjects')
  field1 = .....
  field2 = ....

Projeto管理员

class AditamentoInline(admin.StackedInline):
    form = AditamentoForm
    model = Aditamento
    extra = 0
    max_num = 4


class ProjetoAdmin(admin.ModelAdmin):
    inlines = [AditamentoInline]

change_from_objects_tools.html

{% load i18n admin_urls %}
{% block object-tools-items %}
<li>
    {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
    <a href="{% add_preserved_filters history_url %}" class="historylink">{% trans "History" %}</a>
</li>
  <!-- Something like this -->
 {% for aditamento in Aditamento.all %}
   <a href="" target="_blank">{% trans "New Button" %}</a>
 {% endfor %} 
  <!-- --------------- -->
{% if has_absolute_url %}<li><a href="{{ absolute_url }}" class="viewsitelink">{% trans "View on site" %}</a></li>{% endif %}
{% endblock %}

0 个答案:

没有答案