Let's say I have the following Django models:
class A(models.Model):
keywords = models.ManyToManyField(Keyword)
class B(models.Model):
keywords = models.ManyToManyField(Keyword)
I have an A object. I want to find all B objects where some keyword in the B object is the same as some keyword in the A object.
What is the correct way to write this query?
For my sanity, I'm replacing A and B with Article and Blog, respectively. It makes it a lot easier to parse the related names. So, we have:
class Article(models.Model):
keywords = models.ManyToManyField(Keyword)
class Blog(models.Model):
keywords = models.ManyToManyField(Keyword)
This should work, in one query:
article = Article.objects.all()[0]
Blog.objects.filter(keywords__in=Keyword.objects.filter(articles=article))
Django will combine the Keyword.objects.filter
into the Blog.objects.filter
query, making just one database call.
If a
is an instance of A
, something like that should do:
B.objects.filter(keywords__pk__in=a.keywords.values_list('pk', flat=True))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With