【发布时间】:2021-01-30 08:53:59
【问题描述】:
这是我的industry_employeelist 详细视图,效果很好。
class industry_employeelist(DetailView):
model = Industry
template_name = 'app/industry_employeelist.html'
def get_queryset(self):
return Industry.objects.filter(user=self.request.user)
def get_object(self):
return get_object_or_404(Industry, user=self.request.user)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
this_industry = get_object_or_404(Industry.objects.filter(user=self.request.user))
context['employeeList'] = self.object.employee_set.all()
return context
def post(self, request, *args, **kwargs):
this_industry = get_object_or_404(Industry.objects.filter(user=request.user))
this_name = request.POST['name']
this_rank = request.POST['rank']
this_email = request.POST['email']
this_phone = request.POST['phone']
this_nid = request.POST['nid']
this_address = request.POST['address']
employee_obj = Employee.objects.create(
industry=this_industry, name=this_name, rank=this_rank, email=this_email,
phone=this_phone, nid=this_nid, address=this_address
)
employee_obj.save()
return redirect('app:industry_employeelist')
现在我想为我的项目提出自动搜索建议。正在寻找教程:youtubelink 和 Doc。当我将get 方法添加到industry_employeelist 详细视图时,它不再起作用了。
def get(self, request, *args, **kwargs):
this_industry = get_object_or_404(Industry.objects.filter(user=request.user))
if 'term' in request.GET:
srckey = request.GET.get('term')
suggession = this_industry.employee_set.all().filter(name__contains=srckey)
sug_list = list()
for i in suggession:
sug_list.append(i.name)
return JsonResponse(sug_list, safe=False)
context = super().get_context_data(**kwargs)
请建议如何解决?
【问题讨论】: