0

here is my "Project" Model :

class Project(models.Model):
    name = models.CharField(max_length=200, verbose_name="Project name", default="")

I know how to use QuerySets to filter all the Project objects who contains "result" in their name (like this) :

projects = Project.objects.filter(name__contains=result)

(if I have three projects named "Hello", "Hello_World" and "Hi" and I do this :

hello_projects = Project.objects.filter(name__contains="Hello")

my QuerySet "hello_projects" will contains the projects named "Hello" and "Hello_World")

I want to do it in the opposite way, and filter the projects who have their name in my result.

For example, if "result" is "Hello_World_Its_Me", I want to get all projects who have their names inside result, so "Hello", and "Hello_World"

Is there a way to do it ?

  • Does this answer your question? [Django reverse to contains/icontains](https://stackoverflow.com/questions/15465858/django-reverse-to-contains-icontains) – Harun Yilmaz Sep 02 '22 at 14:20
  • I know this is not really an answer, but consider doing this simply in Python. If you really want to do it in DB then look into https://docs.djangoproject.com/en/4.0/ref/contrib/postgres/search/#trigram-similarity or custom SQL queries – Bartosz Stasiak Sep 02 '22 at 14:40
  • As Bartosz said its probably best to do this in python if you arent going to have huge datasets. This shows how to do it in python or a custom SQL query: https://stackoverflow.com/questions/9003691/django-query-field-is-substring – John-Hugh Hedrick Sep 02 '22 at 14:43

0 Answers0