I have models as below and I would like to select IndexedLibrary objects depending on its book name and the tag names of that book.
How can I build this query? The query below performs without including tags of the book, but I would like to join them also
IndexLibrary.objects.filter(book__name__icontains=KEYWORD)
class IndexedLibrary(models.Model):
name = models.CharField(max_length=1000)
book = models.ForeignKey(Book,null=False,blank=False)
def __unicode__(self):
return self.name
class Book(models.Model):
name = models.CharField(max_length=1000)
def __unicode__(self):
return self.name
class BookTag(models.Model):
name = models.CharField(max_length=1000)
book = models.ForeignKey(Book,null=False,blank=False)
def __unicode__(self):
return self.name
I would add
tagsas aManyToManyField(Tag, blank=True), remove thebookproperty fromBookTagand then query likeEspecially since tags are often shared among more than one object.