My models.py is something like this:
class Department(models.Model):
name = models.CharField(max_length=255)
class Category(models.Model):
name = models.CharField(max_length=255)
department = models.ForeignKey(Department, related_name="categories")
class Product(models.Model):
name = models.CharField(max_length=255)
category = models.ForeignKey(Category, related_name="products")
As you can see, the product model does not have a direct connection to the department, it only has one through the category model. However, in my department details for < Department X >, I want to get all the products that have a category that has the department < Department X >. Is there any way to do it with one query? I don't want to use a for loop for this. Thanks!