[Answered ]-How to make 1:N orm? use prefetch_related?

1👍

You can enumerate with .userportfoliofile_set.all:

{% for portfolioImgList in portfolioList.userportfoliofile_set.all %}
    …
{% endfor %}

this will make a query to only retrieve the UserPortfolioFiles related to the portfolioList object.

In the view you can work with .prefetch_related(…) [Django-doc] to fetch all related items with one query, so:

def some_view(request):
    portfolio_list = UserPortfolio.objects.filter(
        user_idx=request.user.id
    ).prefetch_related('userportfoliofile_set').order_by('-idx')

    return render(request, 'account/setting/portfolio_list.html', {'portfolio_list':portfolio_list})

Leave a comment