[英]Compound generic view in Django?
I'm working on my first Django project. 我正在做我的第一个Django项目。 I need to display a compound page containing both sides of a one-to-many database relationship. 我需要显示一个包含一对多数据库关系双方的复合页面。 Trying to be as Django-y as possible, I considered (class based) generic views. 为了尽可能成为Django-y,我考虑了(基于类的)通用视图。 Since none of the built-in generic views had anything similar, I hacked together a compound generic view using SingleObjectMixin
and MultipleObjectMixin
, which also meant I had to extend the base View
class. 由于没有一个内置的通用视图具有相似的功能,因此我使用SingleObjectMixin
和MultipleObjectMixin
破解了一个复合通用视图,这也意味着我必须扩展基本View
类。
Looking back, this seems like a lot of work for something that (in my opinion) should be quite trivial, and I assumed that I took the wrong path to solve that problem. 回想起来,这似乎是很多工作(对于我认为微不足道的事情),我认为我走了错误的道路来解决该问题。 So I'm turning to the SO community for help: 因此,我正在向SO社区寻求帮助:
Thanks! 谢谢!
I would simply use DetailView
(which uses SingleObjectMixin
), and access the related object through its access attribute on the instance in the template: 我将只使用DetailView
(使用SingleObjectMixin
),并通过其在模板中实例上的访问属性来访问相关对象:
<h2>Main Object</h2>
<p>{{ object.some_field }}</p>
<h2>Related Object</h2>
{% with related=object.related_whatever %}
<p>{{ related.some_field }}</p>
<p>{{ related.other_fied }}</p>
{% endwith %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.