未分配Django SearchRank批注,生产中存在错误,但未开发dev

时间:2019-05-21 12:52:43

标签: django postgresql search annotations django-queryset

我的Django应用程序(带有Postgres后端的Apache / WSGI)在生产中遇到错误,我无法在dev中或通过在生产机器上使用Django shell进行复制。我该如何调试呢?

该应用程序具有一个Story模型

class Story(models.Model): 
    title = models.CharField(max_length=400)
    content = models.TextField(blank=True) 
    quality = models.FloatField(default=0)
    search = SearchVectorField(null=True)

有一个在保存时填充search的信号:

@receiver(post_save, sender=Story) 
def update_search_vector(sender, instance, **kwargs):
   Story.objects.filter(pk=instance.pk).update(search=SearchVector('title', 'content'))

当用户搜索时,我想返回相关的高质量结果。我将得分指定为这两个值的谐波均值:

query = SearchQuery(form.cleaned_data['query'])
stories = stories.annotate(
    rank=SearchRank(F('search'), query),
    score=F('rank') * F('quality') / (F('rank') + F('quality'))
).filter(rank__gte=SEARCH_RANK_CUTOFF).order_by('-score')

这在dev和生产中的Django shell上都可以正常工作。但是,通过正在运行的应用程序执行时,会引发以下错误:django.core.exceptions.FieldError: Cannot resolve keyword 'rank' into field.

我在弄清楚如何调试它时遇到了麻烦。可能是某种错误的查询集缓存吗?

这是完整的错误回溯:

ERROR 2019-05-21 11:59:03,388 Internal Server Error: /stories
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 34, in inner
  response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 115, in _get_response
  response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 113, in _get_response
  response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/myapp/myapp/views.py", line 75, in browse
  score=F('rank') * F('quality') / (F('rank') + F('quality'))
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 1056, in annotate
  clone.query.add_annotation(annotation, alias, is_summary=False)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py", line 1000, in add_annotation
  summarize=is_summary)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/expressions.py", line 446, in resolve_expression
  c.lhs = c.lhs.resolve_expression(query, allow_joins, reuse, summarize, for_save)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/expressions.py", line 446, in resolve_expression
  c.lhs = c.lhs.resolve_expression(query, allow_joins, reuse, summarize, for_save)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/expressions.py", line 511, in resolve_expression
  return query.resolve_ref(self.name, allow_joins, reuse, summarize, simple_col)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py", line 1601, in resolve_ref
  join_info = self.setup_joins(field_list, self.get_meta(), self.get_initial_alias(), can_reuse=reuse)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py", line 1504, in setup_joins
  names[:pivot], opts, allow_many, fail_on_missing=True,
File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/query.py", line 1420, in names_to_path
  "Choices are: %s" % (name, ", ".join(available)))
django.core.exceptions.FieldError: Cannot resolve keyword 'rank' into field. Choices are: content, id, quality, search, title

我安装了相同的依赖项版本(通过requirements.txt),但是我注意到在dev运行3.7.3的同时生产正在运行python3.5.2。 (并且可能libapache2-mod-wsgi-py3调用了另一个版本?)这似乎是一个可以探索的地方,但是我不明白添加rank的注释如何会在没有其他异常的情况下失败。

0 个答案:

没有答案