8

I am getting FieldError: Unsupported lookup 'unaccent' for CharField or join on the field not permitted. Any help is appreciated. Any ideas?

#template
<form class="navbar-form navbar-left" method="GET" action="{% url 'search' %}" value="{{request.get.q}}">
        <div class="form-group">
          <input type="text" name="q" class="form-control" placeholder="Search" >
        </div>
          <button type="submit" class="btn btn-default">Submit</button>
      </form>

#views.py
def Search(request):
    queryset_list=Testimony.objects.all().order_by('timestamp')
    if request.user.is_staff or request.user.is_superuser:
        queryset_list=Testimony.objects.all()

    print('1')
    if request.method=='GET':
        query=request.GET.get("q")
        print('2')
        queryset_list = Testimony.objects.filter(
            Q(title__unaccent__lower__trigram_similar=query)|
            Q(body__unaccent__lower__trigram_similar=query)|
            Q(username__unaccent__lower__trigram_similar=query)
            #Q(firstname__unaccent__lower__trigram_similar__icontains=query)
            ).distinct().order_by('timestamp')


    print('3')
    paginator = Paginator(queryset_list, 20)
    page_request_var="page"
    page=request.GET.get(page_request_var)
    try:
        queryset=paginator.page(page)
    except PageNotAnInteger:
        queryset=paginator.page(1)
    except EmptyPage:
        queryset=paginator.page(paginator_num.pages)

    print('4')
    context={
    "object_list": queryset,
    "title":"list",
    "page_request_var":page_request_var,
    }

    return render(request, 'search.html', {'queryset_list':queryset_list})

Traceback points to: Q(username__unaccent__lower__trigram_similar=query)

markwalker_
  • 12,078
  • 7
  • 62
  • 99
HAN YAO FOONG
  • 137
  • 1
  • 8
  • 3
    Both `unaccent` and `trigram_similar` are provided by the `'django.contrib.postgres'` app. Did you add that to INSTALLED_APPS? – Daniel Roseman Jan 07 '19 at 10:21
  • Thanks! It worked. – HAN YAO FOONG Jan 07 '19 at 15:05
  • Thanks you, for some reason I had to activate these extention on my database, done so by following the answer to this question: https://www.semicolonworld.com/question/84542/fielderror-unsupported-lookup-for-charfield-or-join-on-the-field-not-permitted-on-django – Yorbjörn Apr 05 '22 at 09:44

1 Answers1

12

For Django at least 1.11 (source) and PostgreSQL at least 8.5 (source)

  • Add the line django.contrib.postgres in INSTALLED_APPS (settings.py)
  • Activate the unaccent extension
    • run CREATE EXTENSION unaccent; at PgAdmin (source) or...
    • create an empty migration (./manage.py makemigrations --empty your.application.name) and edit the generated file with the following content (source) and, finally, running ./manage.py migrate:

other reference

from django.contrib.postgres.operations import UnaccentExtension

class Migration(migrations.Migration):

dependencies = [
    (<snip>)
]

operations = [
    UnaccentExtension()
]

Trivia: It didn't work at admin.py, but did worked at other .py files.

Robo
  • 660
  • 5
  • 22
  • Excelent entry!.. Just to add that the empty migration script should be modified to `./manage.py makemigrations --empty your.application.name` – juanmiguelRua Sep 23 '19 at 14:54