我正在尝试了解 Django 的基于类的视图(对它来说非常新),尤其是 ListView。我正在努力理解“业务逻辑应该去哪里”。例如,我有以下课程:
#views.py
class DisplayListView(ListView):
model = Cars
template_name = "searchres_list.html"
paginate_by = '5'
context_object_name = "titles"
def get_context_data(self, **kwargs):
context = super(SearchDisplayListView, self).get_context_data(**kwargs)
# custom logic whoch spits out "now". in this example [1 -->10]
context['now'] = [1,2,3,4,5,6,7,8,9,10]
return context
它工作正常,我可以在我的模板上查看 [1 --> 10]。但是,当我查看ListView下可用的方法时,我发现我可能会将我的逻辑包含在 get_queryset 方法中。所以,像:
def get_queryset(self):
# Fetch the queryset from the parent get_queryset
queryset = super(SearchDisplayListView, self).get_queryset()
# custom logic whoch spits out "now". in this example [1 -->10]
queryset = [1,2,3,4,5,6,7,8,9,10]
return queryset
所以,我相当(愚蠢)的问题是(或者我完全错了!),理想情况下业务逻辑应该去哪里:
- def get_context_data
- def get_queryset
谢谢你的时间。