Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django:如何使用DetailView反转对象?_Django_Python 3.x_Sqlite_Django Generic Views - Fatal编程技术网

Django:如何使用DetailView反转对象?

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()) #

我使用了一般的观点。每一集都通过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())

# 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