1
我想了解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
所以,我宁愿(愚蠢的)问题是(或曾经我得到了这一切完全错误的!),应该在哪里的商业逻辑非常去:
- 高清get_context_data
- def get_queryset
谢谢你的时间。
感谢这么多的一些见解 - 但是我不明白你的第一个问题 - 你是什么意思“,因为在get_queryset方法,你只能修改你的ListView的queryset“你能解释一下吗?正如我所看到的,我可以将查询集或上下文数据传递给模板 - 除了模板标签之外,无论我在哪里放置数据 - 查询集或上下文,一切似乎都正常工作。这不是这种情况吗? – AJW 2013-02-15 11:05:08
所以,当你从''get_queryset''方法改变''queryset''属性时,你基本上会改变模板中的''object_list''。你不能在get_queryset中添加一个表单对象。你需要使用''get_context_data''来实现这一点。 – dragoon 2013-02-15 11:18:47
谢谢 - 我现在明白了!接受你的回答:) – AJW 2013-02-15 11:28:15