Hello all,
I am new on the forum (just 4 days) but I was already helped by KenWhitesell about Celery. Thank you so much.
This time I have a Django question how in elegant way made __in search in insensitive case.
Let’s say I have a model Item
class Item(models.Model):
year = models.CharField(max_length=4, blank=True, null=True)
name = models.CharField(max_length=16, blank=True, null=True)
and also model Tag that contains short texts that describes Item
class Tag(models.Model):
text = models.CharField(max_length=16, blank=True, null=True)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
How in elegant way search for Item based on Tags texts in insensitive way?
Here is View I have created. The ItemSearchListView gets query q which is a list of words, separated by comma, for instance “Blue, white, Yellow, red”. I split it into list and would like to make insensitive Tag search.
class ItemSearchListView(ListView):
model = Item
def get_queryset(self):
q = self.request.GET.get('q')
search_list = q.split(',')
return Item.objects.filter(Q(Tag_set__text__in=search_list)).distinct()
I found one solution on stackoverflow.com but it is not elegant in my opinion and I believe can be done better, because it gets really messy and complicated when search by many different fields of Tag model for instance.
Any idea how to solve the really common problem? Maybe my approach is wrong and it should be done in different way that will allow to get the search in easy way.