Django:如何使用DetailView反转对象?
我使用了一般的观点。每一集都通过ForeignKey连接到某一季。在views.py中,我有以下内容:Django:如何使用DetailView反转对象?,django,python-3.x,sqlite,django-generic-views,Django,Python 3.x,Sqlite,Django Generic Views,我使用了一般的观点。每一集都通过ForeignKey连接到某一季。在views.py中,我有以下内容: class SeasonList(generic.ListView): template_name = 'episodes/episodes.html' context_object_name = 'all_seasons' def get_queryset(self): return reversed(Season.objects.all()) #
class SeasonList(generic.ListView):
template_name = 'episodes/episodes.html'
context_object_name = 'all_seasons'
def get_queryset(self):
return reversed(Season.objects.all())
# Here I need to sort the episodes
class SeasonDetails(generic.DetailView):
model = Season
template_name = 'episodes/season_details.html'
在列表视图中,我使用反转来首先显示最新的一季。类似地,在detail视图中,我希望片段按降序显示,因为最新的片段应该显示在页面顶部
在我的html中,我使用seasure.spice\u set.all访问了剧集列表
{% for episode in season.episode_set.all %}
<!-- the tags to show the list -->
{% endfor %}
有什么方法可以反转插曲列表吗?您可以按id排序,并根据需要使用后代或升序
Season.objects.all().order("id") # Ascendant
Season.objects.all().order("-id") # Decendant
要对剧集进行排序,可以使用以下内容-
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['episodes'] = Episodes.objects.all().order_by('-date_posted')
return context
但它将逆转季节的目标。我已经做过了。事实上,我正在寻找一种方法来扭转这一幕。我用外键把这些插曲和某个季节联系起来。对不起,如果我没有正确描述我的问题。
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['episodes'] = Episodes.objects.all().order_by('-date_posted')
return context