Multiple models generic DetailView to template

一曲冷凌霜 提交于 2019-12-01 08:37:14

Explanation of why you can't use one view for both models

A DetailView is meant to display details about an object from one model. It's fine to include extra context, but the view isn't designed to handle two possible models.

The example from the docs is showing the details for one publisher, and displaying all the books at the same time.

Your DetailView lets you show the details for one CharacterSeries, and display all of the CharacterUniverse at the same time.

However, you cannot use that same view to display details for one CharacterUniverse. You need a different view to display details for one CharacterUniverse

Solution

Therefore, you need two different detail views, one for each model.

You need a distinct url for each view. Otherwise, the request will always match the first regex (in this case series_detail. The following would work.

url(r'^series/(?P<pk>[0-9]+)/$', views.SeriesDetail.as_view(), name='series_detail'),
url(r'^universe/(?P<pk>[0-9]+)/$', views.UniverseDetail.as_view(), name='universe_detail'),
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!