【发布时间】:2019-02-07 19:52:57
【问题描述】:
我有三个模型,例如 Store、ProductsArea 和 Item。一个 Store 可以有很多 ProductsArea,ProductsArea 可以有很多 Item。
#models.py
class Store(models.Model):
name = models.CharField(max_length=64, unique=True)
description = models.TextField(null=True, blank=True)
class ProductArea(models.Model):
store = models.ForeignKey(Store, related_name='productarea', on_delete=models.CASCADE)
name = models.CharField(max_length=64,verbose_name=_('Name'))
class Item(models.Model):
product_area = models.ForeignKey(ProductArea, related_name='items', on_delete=models.CASCADE, verbose_name='Menu')
name = models.CharField(max_length=64, verbose_name=_('Name'))
price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name=_('Price'))
在我看来,我正在使用 CBV,我想返回包含相同 ProductArea 的所有项目,单击确定的 ProductArea 中的 ListItems,使用 FK 他们从 ProductArea 中获取所有项目。 Store 中的 ProductArea 也是如此。
Store - Clothes Store
Product Area - Shirts, Pants
Item - Shirt Yellow(PA.Shirts), Shirt Blue(PA.Shirts)
Item - Pant Black(PA.Pants), Pants Red(PA.Pants)
如果点击Shirts,我想返回一个包含Shirt Yellow 和Shirt Blue 的列表。
我在我看来尝试过这个:
def get_queryset(self):
product_area_id = ProductArea.objects.get(id)
product_area = ProductArea.objects.get(pk=product_area_id)
items_in_productarea = Item.objects.filter(product_area=product_area)
return items_in_productarea
但是不行,有builtin_function_or_method' object is not iterable
谁能帮我?非常感谢朋友们。
【问题讨论】:
-
首先
id不是不是 URL的id(请同时发布您的相关url),它是身份功能。
标签: django django-models django-queryset django-filter