【发布时间】:2011-06-05 12:41:32
【问题描述】:
我有两个未绑定的模型:
class News(models.Model):
name = models.CharField(max_length=255, blank=True, null=True)
text = models.TextField(blank=True, null=True)
country = models.IntegerField(max_length=255, blank=True, null=True) # 1, 2, 3, etc.
class Countries(models.Model):
name = models.CharField(max_length=255, blank=True, null=True) # USA, Canada
我知道这是脏代码,我应该使用ForeignKey,但不幸的是,我无权触摸models.py文件。
如何将其组合成一个列表?
def show_news(request):
news_list = News.objects.all()
countries_list = Countries.objects.all()
# like news_list = news_list + countries_list
return render(request, 'table_adsl.html', {'news_list': news_list})
并在模板中显示:
{% for news in news_list %}
<h2>{{news.title}} - {{news.country}}</h2>
...
{% endfor %}
然后得到类似:<h2>Beer Festival - Germany</h2>?
【问题讨论】:
标签: django django-views