我正在开始一个新的应用程序,我正在尽力接受基于类的视图。啊,成长的痛苦。我试图从GET变量做一个简单的过滤器,如果它不存在我想返回id desc排序的所有对象,如果是,我想过滤,排序并返回过滤后的列表。
我正在编写30多行代码来执行此操作,因此我必须做错事,对吧?我尝试重写get_queryset(),现在我想起来了,也许我应该在get_queryset()中调用self.request.GET ['search']来进行这种过滤。有没有一种标准的方法来实现这个目标?
class MyModelList(AdminPageMixin, ListView):
model = MyModel
context_object_name = 'object'
template_name = 'template/list.html'
def get(self, request, *args, **kwargs):
if 'search' in request.GET and len(request.GET['search']):
search = request.GET['search']
self.object_list = MyModel.objects.filter(advertiser__name=search).orderby('-id')
context = self.get_context_data(object_list=self.object_list, search=search)
else:
self.object_list = MyModel.objects.all()
context = self.get_context_data(object_list=self.object_list).orderby('-id')
return self.render_to_response(context)
def get_context_data(self, **kwargs):
context = super(MyModelList, self).get_context_data(**kwargs)
form = MyModelForm
try:
context['search'] = kwargs['search']
except KeyError:
pass
context['form'] = form
context['form_action'] = reverse('mymodel-add')
context['form_display'] = 'hide'
context['form_save_label'] = 'Add'
return context
其次,为了使问题复杂化,我有一个CreateView和一个UpdateView,我想分享我的ListView的get()或get_queryset()和get_context_data()方法。我确信使用Mixin是可能的,但同样,我正在寻找一种标准化的方法。建议?这是CreateView,我想在其中分享ListView的方法。
class MyModelAdd(AdminPageMixin, CreateView):
model = MyModel
form_class = MyModelForm
context_object_name = 'object'
template_name = 'templates/list.html'
def get_success_url(self):
return reverse('mymodel-list')
def get_context_data(self, **kwargs):
context = super(MyModelAdd, self).get_context_data(**kwargs)
context['form_action'] = reverse('mymodel-add')
context['form_display'] = 'show'
context['object_list'] = MyModel.objects.all()
return context
def form_invalid(self, form):
form = MyModelForm(self.request.POST)
context = self.get_context_data()
context['form'] = form
context['form_action'] = reverse('mymodel-add')
context['form_display'] = 'show'
context['form_save_label'] = 'Add'
return render(self.request, 'templates/list.html', context)
答案 0 :(得分:3)
首先,您的观察是正确的:您绝对应该使用get_queryset
来过滤列表。我建议采取以下措施:
class MyModelList(AdminPageMixin, ListView):
model = MyModel
context_object_name = 'object'
template_name = 'template/list.html'
def get_queryset(self):
qs = self.model.objects.all()
search = self.request.GET.get('search')
if search:
qs = qs.filter(advertiser__name__icontains=search)
qs = qs.order_by("-id") # you don't need this if you set up your ordering on the model
return qs
其次,如果你真的需要在List和其他视图之间重用代码,那么最好编写一个单独的函数而不是使用mixin。
附注:您的表单中不需要form_action
网址;只需设置action=""
,它就会POST到相同的网址。
答案 1 :(得分:0)
最好的方法是过滤'get_context_data'中的数据。
def get_context_data(self, **kwargs):
context = super(MyModelAdd, self).get_context_data(**kwargs)
context['form_action'] = reverse('mymodel-add')
context['form_display'] = 'show'
context['object_list'] = MyModel.objects.all(user=self.request.user)# that's will filter the user data
return context