簡體   English   中英

過濾外鍵值django

[英]filter on a foreign key value 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。

例如:books是Product_category.collection ,我想獲得書籍收藏的所有產品。

我也試過了__方法。 不知怎的,它不起作用。

雙下划線是要走的路。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM