[Answered ]-How merge two queries from unrelated models

2๐Ÿ‘

โœ…

I understand that News.country refers to Country.id. So you could do something like that:

def show_news(request):
    news_list = News.objects.all()
    countries_list = Countries.objects.all()
    countries = dict([(c.id, c.name) for c in countries_list])
    for n in news_list:
        n.country_name = countries.get(n.country, '(no country)')
    return render(request, 'table_adsl.html', {'news_list': news_list})

and

{% for news in news_list %}
    <h2>{{news.title}} - {{news.country_name}}</h2>
    ...
{% endfor %}

Doing it in the database would be faster, but this is an easy way and premature optimization โ€ฆ

๐Ÿ‘คjammon

Leave a comment