【问题标题】:django error while searching in unaccent way以非重音方式搜索时出现django错误
【发布时间】:2018-12-24 07:38:37
【问题描述】:

models.py

class KeySkills(models.Model):
    skills            = models.TextField()
    versions          = models.DecimalField(decimal_places=3,null=True,blank=True,max_digits=10,default=None)
    experience        = models.DecimalField(decimal_places=3,null=True,blank=True,max_digits=10,default=None)
    user              = models.ForeignKey(access_models.SeekerRegister,on_delete=models.CASCADE,related_name='key_skills',null=True,blank=True)

    def __str__(self):
        return "KeySkills"

查询:

KeySkills.objects.filter(skills__unaccent__icontains='python')
KeySkills.objects.filter(skills_text__search='python')

错误:

  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 844, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 862, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1263, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1287, in _add_q
    split_subq=split_subq,
  File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1225, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1080, in build_lookup
    lhs = self.try_transform(lhs, name)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1126, in try_transform
    (name, lhs.output_field.__class__.__name__))
django.core.exceptions.FieldError: Unsupported lookup 'unaccent' for TextField or join on the field not permitted.

我在这里尝试使用 django ORM 查询进行搜索。 并使用上述两种查询方式进行尝试,但我遇到了错误。

请查看我的代码。

【问题讨论】:

    标签: django


    【解决方案1】:

    您需要在您的INSTALLED_APPS 中添加'django.contrib.postgres' 另外,试试这个link

    【讨论】:

    • 我正在使用 mongodb
    【解决方案2】:

    您需要在 INSTALLED_APPS 中添加“django.contrib.postgres”: 然后你需要为你的数据库安装扩展“unaccent”来执行这个查询:

    CREATE EXTENSION unaccent;
    

    我在执行查询中的 pgadmin4 中执行了此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 2012-01-28
      • 2018-08-08
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多