Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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 在listview类方法中包含业务逻辑的位置_Django_Django Views_Django Class Based Views - Fatal编程技术网

Django 在listview类方法中包含业务逻辑的位置

Django 在listview类方法中包含业务逻辑的位置,django,django-views,django-class-based-views,Django,Django Views,Django Class Based Views,我试图理解Django的基于类的视图(对它来说非常陌生),尤其是ListView。我正在努力理解“业务逻辑应该走向何方”。例如,我有以下课程: #views.py class DisplayListView(ListView): model = Cars template_name = "searchres_list.html" paginate_by = '5' context_object_name = "titles" def get_context_da

我试图理解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]。然而,当我查看下可用的方法时,我发现我可能会在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获取上下文数据
  • def get_queryset

  • 谢谢您的时间。

    对于这样一个主观问题,最好的答案可能是:视情况而定

    我个人处理这种情况的算法如下:

    • 如果您需要向将传递到模板的上下文添加内容,那么实际上您没有选择余地,因为在get\u queryset方法中,您只能修改ListView的queryset。所以我在本例中使用get_context_数据
    • 但是,如果您要执行一些动态queryset修改,假设您的视图可以在类似的模型类上操作,并且实际的类由传递到视图中的参数确定,那么您可能需要覆盖get\u queryset方法

    希望我能给你一些关于这个主题的见解:)

    非常感谢-但是我不理解你的第一点-你说的“因为在get_queryset方法中你只能修改ListView的queryset”是什么意思?你能解释一下吗?。在我看来,我可以将queryset或上下文数据传递给模板-除了模板标记之外,一切似乎都正常工作-无论我将数据放置在何处-queryset或上下文。不是这样吗?因此,当您从
    get\u queryset
    方法更改
    queryset
    属性时,基本上更改了模板中的
    object\u list
    。比如说,您不能从
    get\u queryset
    向上下文添加表单对象。您需要使用
    get\u context\u data
    来实现这一点。谢谢-我现在明白了!接受你的回答:)