对外键值django进行筛选



我很难为我的项目找到正确的查询。以下是我的模型示例:

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

我正在尝试根据Product_Category.collection的值获取所有product_id。

图书是Product_category.collection,我想得到所有的图书收藏产品。

我也试过__方法。不知怎么的,它不起作用。

使用双下划线。

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

最新更新