0
이 의사 모델 고려 :장고 ORM 개체 수
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
솔루션 :
내가 시므온 프랜트의 사용 ' 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()])
(210)
나는 이해할 수 없다. 총 상자 수 또는 총 항목 수를 찾고 있습니까? 'items_in_box'와'quantity_available'은 어떻게 관련이 있습니까? –
데이터베이스에서'BaseProduct'에서'Box'에 상속을 어떻게 관리합니까? 문서에 설명 된 방법 중 하나를 사용합니까? https://docs.djangoproject.com/en/dev/topics/db/models/#model-inheritance –
BaseProduct에는 quantity_available 필드 (정수)가 있습니다. Box는 BaseProduct의 상속이며 Box는 X 제품을 보유 할 수 있습니다. 우산, 상자 및 상자 당 판매한다고 가정 해보십시오. 나는 우산의 총 재고를 원합니다. –