Python Django相关字段的lookup:name无效,因为我没有提到name

Python Django相关字段的lookup:name无效,因为我没有提到name,python,django,Python,Django,您好,我在Django项目中有一个视图,它基本上使用搜索查询来显示分页的结果列表。在开发模式中,这种“查询”功能工作得非常好 但是,当在生产环境中部署时,当尝试进行查询时,模板将返回错误 Related Field got invalid lookup: name 虽然我的views.py中没有我使用“name”这个词的地方。根据回溯交互视图,错误似乎发生在分页函数的开始处 Traceback: File "/home/ubuntu/myproject/env/lib/python3.5/s

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

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

Related Field got invalid lookup: name
虽然我的views.py中没有我使用“name”这个词的地方。根据回溯交互视图,错误似乎发生在分页函数的开始处

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)

模型在哪里?产品的模型还是“项目”fk的模型?或者两者都有?在这里添加模型以获得良好的效果answer@rtindru请确保您正在运行发布的代码。确保已保存/部署更改并重新启动服务器。