Django reverse to contains/icontains

前端 未结 4 1764
星月不相逢
星月不相逢 2021-01-04 06:11

In this question was solved problem for reverse LIKE operation in SQL, for example if field name is \"Peter Johnson\", we could find it by such query:



        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-04 06:41

    If you are on the latest version of Django (1.10 or later) and using Postgres the ORM can handle this. Check out the docs.

    A trigram_similar lookup will get you what you are looking for:

    qs = MyModel.objects.filter(name__trigram_similar='Mr. Peter Johnson')
    

    Don't forget to enable this lookup by enabling the pg_tgrm extension. You can do that with a django migration.

    And you will need to add 'django.contrib.postgres' to your INSTALLED_APPS setting.

提交回复
热议问题