0

I have three models. How to do a search method to browse my three models together. For example if i search for the word "bluebird" in the search bar, i want it to browse through the three models to return the result to me. I saw on a forum the chain method that can be imported from itertools but i don't know how to use it !

models.py

class Category(models.Model):
    name = models.CharField(max_length=250)
    description = models.TextField(blank=True)

    def __str__(self):
        return self.name


class Author(models.Model):
    name = models.CharField(max_length=250)
    photo = models.ImageField(upload_to='pics/authors/', default='pics/default-author.jpg')
    biography = models.TextField(blank=True)

    def __str__(self):
        return self.name


class Book(models.Model):
    author = models.ManyToManyField(Author)
    editor = models.CharField(max_length=250, blank=True)
    title = models.CharField(max_length=250)
    description = models.TextField(blank=True)
    date_added = models.DateField(auto_now_add=True)
    updated = models.DateField(auto_now=True)
    cover = models.ImageField(upload_to='pics/covers/', default='pics/default-cover.jpg')
    pdf_file = models.FileField(upload_to='pdfs/books/', default='pdfs/default-pdf.pdf')
    categories = models.ForeignKey(Category, on_delete=models.CASCADE)

    def __str__(self):
        return self.title
Abdoulaye
  • 27
  • 4
  • Does this answer your question? [How can I combine two or more querysets in a Django view?](https://stackoverflow.com/questions/431628/how-can-i-combine-two-or-more-querysets-in-a-django-view) – Edmond Sep 12 '21 at 16:42

0 Answers0