【发布时间】:2012-03-29 14:03:02
【问题描述】:
考虑这些伪模型:
class BaseProduct:
quantity_available = Integer
class Box(BaseProduct):
items_in_box = Integer
>> BaseProduct.objects.count()
>> Integer
但是我如何检索产品总数,所以:
for each object[quantity_available * items_in_box] * total_objects
解决方案:
我使用 Simeon Visser 的 'sum' 作为部分解决方案,将属性添加到 Base 类:
@property
def _box_count(self):
try:
return self.items_in_box * self.quantity_available
except AttributeError:
return self.quantity_available
sum([item._box_count for item in BaseProduct.objects.all()])
【问题讨论】:
-
我不明白。您是在寻找箱子的总数,还是物品的总数?
items_in_box和quantity_available有什么关系? -
如何管理数据库中从
BaseProduct到Box的继承?您是否使用文档中概述的方法之一? docs.djangoproject.com/en/dev/topics/db/models/… -
一个 BaseProduct 有一个 quantity_available 字段(整数)。 Box 是 BaseProduct 的继承,Box 也可以容纳 X 个产品。假设您销售雨伞,每件和每盒。我想要雨伞的总库存
-
不,我定义了一个自定义 as_leaf_class,类似于 djangosnippets.org/snippets/1034
-
那你有什么? Umbrella(BaseProduct) 和 BoxOfUmbrellas(BaseProduct) 还是别的什么?
标签: python django django-orm