5

我有一个 Django 通用视图 (ListView),我想添加选项以通过其方法对对象进行排序,这些方法稍微复杂,并且可能难以使用extra(). 我意识到这样做的唯一方法是 sort() 查询集,如下所示:

sorted(queryset, key=lambda q: q.choice_entropy())

但是,要将其合并到 ListView 中,并允许用户选择排序标准,我需要这样的东西

class IndexView(ListView):
    model = Question

    def get_queryset(self):
        queryset = super(IndexView, self).get_queryset()
        if 'order_by' in self.request.GET:
            if self.request.GET['order_by'] == 'entropy':
                return sorted(queryset, key=lambda q: q.choice_entropy())
            elif self.request.GET['order_by'] == 'newest':
                return queryset.order_by('-published_time')
            elif self.request.GET['order_by'] == ... (some other sorting criterion)
        return queryset

这里的问题(大概)是sorted(queryset, key=lambda q: q.choice_entropy())返回的一段<type 'list'>时间queryset是类型<class 'django.db.models.query.QuerySet'>

我不认为这会是一个问题,因为self.queryset实际上可以是任何可迭代的项目,而不仅仅是一个查询集。但 ListView 似乎不喜欢这样,我只是得到A server error occurred. Please contact the administrator.了以下回溯:

Internal Server Error: /
Traceback (most recent call last):
  File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/core/handlers/base.py", line 140, in get_response
    response = response.render()
  File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/template/response.py", line 80, in rendered_content
    template = self.resolve_template(self.template_name)
  File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/template/response.py", line 56, in resolve_template
    return loader.select_template(template)
  File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/template/loader.py", line 184, in select_template
    raise TemplateDoesNotExist("No template names provided")
TemplateDoesNotExist: No template names provided
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 72, in __call__
    return self.application(environ, start_response)
  File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
    response = self.get_response(request)
  File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/core/handlers/base.py", line 178, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/core/handlers/base.py", line 217, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/views/debug.py", line 69, in technical_500_response
    html = reporter.get_traceback_html()
  File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/views/debug.py", line 297, in get_traceback_html
    c = Context(self.get_traceback_data())
  File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/views/debug.py", line 237, in get_traceback_data
    for loader in template_source_loaders:
TypeError: 'NoneType' object is not iterable

我无法从中收集到底是什么,NoneType因为我已经在自定义manage.py命令中测试了排序并得到了排序,尽管 Python 列表结果,所以我真的不相信它与对象类型有关。

可能是我似乎想念的明显明显的东西。有任何想法吗?

我已经考虑过尝试将list背面投射到 aQuerySet但据说这是不可能的

我想如果无法调试上述内容,问题就变成了,我怎样才能修改视图以不需要查询集对象,而只使用应该不关心它是查询集还是列表的列表。

4

0 回答 0