我正在使用Django v1.4,我正在尝试子类化通用ListView视图。这是代码
from django.views.generic import ListView
class SearchListView(ListView):
model = None
fields = None
def get_queryset(self):
#...etc...
return super(SearchListView, self).get_queryset()
然后我将进一步为特定模型定制该视图:
class PersonSearchListView(SearchListView):
model = Person
fields = ['first_name', 'last_name']
所以会发生什么,ImproperlyConfigured异常是超类(ListView),声明应该定义模型或查询集。我以为我是......(模特=人)。为什么这个值没有进入视图?
由于
答案 0 :(得分:0)
致电super(SearchListView, self).get_queryset()
您将调用以下类的get_queryset,因为您可以看到如果未设置模型或查询集,它将引发异常。
ListView是MultipleObjectMixin的子项。
但是,如果您实例化PersonSearchListView,则应该正确设置模型。你能包括url配置吗?稍后会尝试并更新我的答案。
class MultipleObjectMixin(ContextMixin):
"""
A mixin for views manipulating multiple objects.
"""
allow_empty = True
queryset = None
model = None
paginate_by = None
context_object_name = None
paginator_class = Paginator
def get_queryset(self):
"""
Get the list of items for this view. This must be an iterable, and may
be a queryset (in which qs-specific behavior will be enabled).
"""
if self.queryset is not None:
queryset = self.queryset
if hasattr(queryset, '_clone'):
queryset = queryset._clone()
elif self.model is not None:
queryset = self.model._default_manager.all()
else:
raise ImproperlyConfigured("'%s' must define 'queryset' or 'model'"
% self.__class__.__name__)
return queryset