Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取django中的多个关联记录_Python_Django - Fatal编程技术网

Python 如何获取django中的多个关联记录

Python 如何获取django中的多个关联记录,python,django,Python,Django,我有一个事件表和另一个表event_performance_details,每个事件可以有多个性能,因此我必须找到所有性能属于特定事件,同时从事件表中获取记录 events = Event.objects.all().filter(event_start_to__gte=today, status=True, current_step=3).order_by('-id')[:3] for event in events: # Here i have to get the records

我有一个事件表和另一个表event_performance_details,每个事件可以有多个性能,因此我必须找到所有性能属于特定事件,同时从事件表中获取记录

events = Event.objects.all().filter(event_start_to__gte=today, status=True, current_step=3).order_by('-id')[:3]
for event in events:
    # Here i have to get the records of event performances 
在event_performance_details表中,我使用event_id作为events表记录的外键

编辑:这是我的模板代码,我必须在其中使用嵌套循环来执行事件

{% for event in events %}
    <p class="date-para">
    {% with performances = event.event_performance_details_set.all() %}
        {% for performance in performances %}
            {{ performance.city }}
        {% endfor %}
    {% endwith %}
    </p>
{% endfor %}
{%for events%}

{%with performances=event.event\u performance\u details\u set.all()%} {性能百分比中的性能百分比} {{performance.city} {%endfor%} {%endwith%}

{%endfor%}
您可以使用反向关系检索每个事件的性能,如下所示:

events = Event.objects.all().filter(event_start_to__gte=today, status=True, current_step=3).order_by('-id')[:3]
for event in events:
    event.event_performance_details_set.all() # returns performances for the event
像Andrey Zarubin指出的那样编辑模板。应该是:

{% with performances = event.event_performance_details_set.all %}
    {% for performance in performances %}
        {{ performance.city }}
    {% endfor %}
{% endwith %}

您可以使用反向关系检索每个事件的性能,如下所示:

events = Event.objects.all().filter(event_start_to__gte=today, status=True, current_step=3).order_by('-id')[:3]
for event in events:
    event.event_performance_details_set.all() # returns performances for the event
像Andrey Zarubin指出的那样编辑模板。应该是:

{% with performances = event.event_performance_details_set.all %}
    {% for performance in performances %}
        {{ performance.city }}
    {% endfor %}
{% endwith %}

它在视图中工作但现在在django模板中工作它在视图中工作但现在在django模板中工作问题是
{%with performances=event.event\u performance\u details\u set.all()%}
。它应该是
{%with performances=event.event\u performance\u details\u set.all%}
而不是谢谢@AndreyZarubin正在工作问题是
{%with performances=event.event\u performance\u details\u set.all()}
。它应该是
{%with performances=event.event\u performance\u details\u set.all%}
insteadThank@AndreyZarubin正在工作