To clarify, I'm not asking for Article.objects.filter(headline__in=['Doge', 'Cat', 'Lizard'])
instead I want to do it the other way around. Kinda like this logic
q = Q()
for word in headline.split():
q |= Q(keywords__keyword__icontains=word)
KeywordSubscription.objects.filter(q).all()
Something akin to that. I'm not sure if there's a shortcut or maybe some sort of function that gives me access to what I need.
Input: 'Cats are taking over the world!'
My table is a mapping between User and Keywords (many to many field):
class KeywordSubscription(models.Model):
user = models.OneToOneField(User, unique=True)
keywords = models.ManyToManyField(Keyword)
objects = KeywordSubscriptionManager()
class Keyword(models.Model):
keyword = models.CharField(max_length=128)
Each user can be subscribed to a couple of keywords, so for example say that user_0 is subscribed to cat, and user_1 is subscribed to dog, the above query would be looping over: ['Cats', 'are', 'taking', 'over', 'the', 'world'] and checking for the user that's subscribed to any of those words
Clarification 2: The code works, it's just not elegant.
EDIT:
it ended up looking like so
query = reduce(operator.or_, (Q(keywords__keyword__iexact=word) for word in instance.headline.split()))