Multiple models generic ListView to template

前端 未结 2 481
天命终不由人
天命终不由人 2020-12-29 00:24

What is the SIMPLEST method for getting 2 models to be listed in a generic IndexView? My two models are CharacterSeries and CharacterUniverse.

2条回答
  •  没有蜡笔的小新
    2020-12-29 00:45

    You can pass the one queryset in as context on the ListView like this,

    class IndexView(generic.ListView):
        template_name = 'character/index.html'
        context_object_name = 'character_series_list'
        model = CharacterSeries
    
        def get_context_data(self, **kwargs):
            context = super(IndexView, self).get_context_data(**kwargs)
            context.update({
                'character_universe_list': CharacterUniverse.objects.order_by('name'),
                'more_context': Model.objects.all(),
            })
            return context
    
        def get_queryset(self):
            return CharacterSeries.objects.order_by('name')
    

提交回复
热议问题