【问题标题】:Get method in Django's detailviewDjango详细视图中的获取方法
【发布时间】: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')

现在我想为我的项目提出自动搜索建议。正在寻找教程:youtubelinkDoc。当我将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)

请建议如何解决?

【问题讨论】:

    标签: python jquery django


    【解决方案1】:

    您将返回 suggestion 中的第一个元素,并且仅当您的 GET 请求中有搜索词时。当您可以使用已经定义的 get_object() 时,您也可以使用 get_object_or_404 来重复自己。

    这是您的代码的优化版本:

    def get(self, request, *args, **kwargs):
      if 'term' in request.GET:
          # Empty list to be populated and returned
          sug_list = list() 
    
          # Make use of the already defined method and only fetch if there's a term in the request
          this_industry = self.get_object() 
    
          # Get the search term
          srckey = request.GET.get('term')
    
          # You don't need to fetch everything using all() to filter it out, using values_list will return the column you need which is name in this case
          sug_list = list(this_industry.employee_set.filter(name__contains=srckey).values_list('name', flat=True)) 
    
          # Return the sug_list
          return JsonResponse(sug_list, safe=False)
    
      # If we request the page without the term parameter render a response
      return super().get(request, *args, **kwargs)
    

    注意:您可以通过在没有对象时丢弃 404 错误并简单地返回一个空列表来进一步改进它。

    【讨论】:

    • 我把这段代码放在我的industry_employeelist 视图中,现在它呈现了一个只有[] 的空白页面!
    • 因此,只有在请求中有 term 时,您才希望返回 JsonResponse。我会相应地编辑我的答案。
    猜你喜欢
    • 2019-12-29
    • 2020-10-30
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多