我有两个型号分别为TvShow和Episode的剧集,但情节通过外键与tvshow连接
tvshow = models.ForeignKey(Media, on_delete=models.CASCADE)
所以我试图将其添加到这样的情节模型的网址中
path('<str:tvshow_title>/<int:season_number>/<int:episode_number>', views.episode, name='episode')
但是Episode的views.py很难定义它:
def episode(request, tvshow_title, season_number, episode_number):
tvshow = Media.objects.filter(media_type='TV', title=tvshow_title).first()
episode = get_object_or_404(
Episode, tvshow=tvshow, season_number=season_number, episode_number=episode_number)
context = {
'episode': episode
}
return render(request, 'media/episode.html', context)
我不确定是什么问题,我不确定这是否是您将多个参数传递给get_object_or_404的方式。
答案 0 :(得分:0)
跟随Django documentation,您可以直接在get_object_or_404
中使用过滤器
episode = get_object_or_404(
Episode, tvshow__title=tvshow_title, tvshow__media_type='TV', season_number=season_number, episode_number=episode_number)