filter on a foreign key value django

I'm struggling getting the right query for my project. Here is an example of my model :

from django.db import models

class Product_Category(models.Model):
    name = models.CharField(max_length=30, unique=True)
    handle = models.SlugField(max_length=30, unique=True, null=True, blank=True)
    collection = models.CharField(max_length=30, null=True)
    def __unicode__(self):

class Product(models.Model):
    product_id = models.SlugField(unique=True)
    name = models.CharField(max_length=100)
    collections = models.ManyToManyField('Collection')
    category = models.ForeignKey(Product_Category, on_delete=models.SET_NULL, blank=True, null=True)
    def __unicode__(self):
        return self.product_id

I am trying to get all the product_id based on the value of the Product_Category.collection.

Eg: books is a Product_category.collection, I want to get all the products for books collection.

I tried __ method also. Somehow it is not working.


The double underscore is the way to go.

books = Product.objects.filter(category__collection='books')

Need Your Help

How can I add Applicasas SDK to Android Studio

android android-studio

I am trying to add Applicasas SDK to Android Studio. Applicasas Start guide just covers Eclipse, and I have read it multiple times, bu...

Throwing back Exception in catch block asks for another try catch block

java exception exception-handling

I am trying to throw Exception Back to calling one. so i am catching the exception and throwing it . while throwing it ask me to surround the statement in try catch block.