Django - 相关字段无效查找:我没有提到姓名

时间:2018-04-20 12:00:39

标签: python django

嗨我在我的Django项目中有一个视图,它基本上使用搜索查询来显示分页的结果列表。在开发模式下,这种“查询”功能非常适用。

但是,在生产中部署时,尝试进行查询时,模板会返回错误

Related Field got invalid lookup: name

虽然我的views.py中没有任何地方使用“name”这个词。根据tracebacks交互视图,错误似乎发生在分页函数的开头。

Traceback:

File "/home/ubuntu/myproject/env/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/home/ubuntu/myproject/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/home/ubuntu/myproject/env/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/ubuntu/myproject/products/views.py" in results
  129.     def paginator_loop(arg):

File "/home/ubuntu/myproject/env/lib/python3.5/site-packages/django/db/models/manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/home/ubuntu/myproject/env/lib/python3.5/site-packages/django/db/models/query.py" in filter
  836.         return self._filter_or_exclude(False, *args, **kwargs)

File "/home/ubuntu/myproject/env/lib/python3.5/site-packages/django/db/models/query.py" in _filter_or_exclude
  854.             clone.query.add_q(Q(*args, **kwargs))

File "/home/ubuntu/myproject/env/lib/python3.5/site-packages/django/db/models/sql/query.py" in add_q
  1253.         clause, _ = self._add_q(q_object, self.used_aliases)

File "/home/ubuntu/myproject/env/lib/python3.5/site-packages/django/db/models/sql/query.py" in _add_q
  1271.                     current_negated, allow_joins, split_subq)

File "/home/ubuntu/myproject/env/lib/python3.5/site-packages/django/db/models/sql/query.py" in _add_q
  1277.                     split_subq=split_subq,

File "/home/ubuntu/myproject/env/lib/python3.5/site-packages/django/db/models/sql/query.py" in build_filter
  1207.                 raise FieldError('Related Field got invalid lookup: {}'.format(lookups[0]))

Exception Type: FieldError at /results/
Exception Value: Related Field got invalid lookup: name

views.py的简化版本:

def results(request):
    user = request.user
    q = request.GET.get('q')
    prod = Product.objects.filter(
        Q(item__description__icontains=q)|
        Q(item__category__category_title__icontains=q)) #note that name IS NOT at all mentioned here
    prod = prod.distinct()
    prod = prod.order_by('-rating')
    def paginator_loop(arg):
        try:
            prod = arg.page(p)
        except PageNotAnInteger:
            prod = arg.page(1)
        except EmptyPage:
            prod = arg.page(paginator.num_pages)
        return prod
    paginator = Paginator(prod, 20)
    prod = paginator_loop(paginator)
    return render(request, 'results.html', {'user':user,'prod':prod})

编辑: 以下是我的分类,产品和项目模型......

class Category(models.Model):
    name = models.CharField(max_length=8) # Name is here on this model, though it is not called in the filter
    category_title = models.CharField(max_length=30,null=True) 

class Item(models.Model):
    description = models.TextField(null=True)
    category = models.ForeignKey(Category,related_name='c_item',null=True,on_delete=models.SET_NULL) 

class Product(models.Model):
    parent_item = models.ForeignKey(Item,related_name='i_product',null=True,on_delete=models.SET_NULL)
    slug = models.SlugKey(unique=True)
    dimensions_h = models.DecimalField(max_digits=9,decimal_places=2,null=True)
    dimensions_w = models.DecimalField(max_digits=9,decimal_places=2,null=True)

0 个答案:

没有答案