Reputation: 478
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):
return self.name
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.
Upvotes: 1
Views: 122
Reputation: 600051
The double underscore is the way to go.
books = Product.objects.filter(category__collection='books')
Upvotes: 3