Django Search for many searches

I am trying to execute a search query using ManyToMany Relationships, This is what I still have:

designs = designs.filter(Q(title__icontains = search) | Q(tags__icontains = search)) 

Do you know how I can search the tags.title field in a query?

Here are the models, I cleaned them, so they arent so long :)

 class Design(models.Model): title = models.CharField(max_length = 50, default = "") slug = models.SlugField(unique = True) user = models.ForeignKey(User, related_name = "design_user") description = models.TextField() tags = models.ManyToManyField(to = Tags) class Tags(models.Model): title = models.CharField(max_length = 50, unique = True) # Allows the category to list as a dropdown in the admin def __unicode__(self): return self.title 

Most of the questions I was looking for use filters, and I am not a Django master, so I ask this, hopefully not add a duplicate question.

+4
source share
2 answers

Do this with the correct field_lookup : tags__title__icontains = search :

 designs = designs.filter(Q(title__icontains = search) | Q(tags__icontains = search) | Q(tags__title__icontains = search)) 

Finding fields is very useful, you should take a look at the documents .

+8
source

You can do this using tags__title__icontains as

 designs = designs.filter(Q(title__icontains = search) | Q(tags__title__icontains = search)) 
0
source

Source: https://habr.com/ru/post/1481385/


All Articles