제품의 가용성과 그 변형을 확인하는 기능이 있습니다. 두 모델의 필드는 quantity_left
입니다.목록 내재 상태의 내부 루프 조건
제품에 변형이 있으면 quantity_left
을 변형하고 싶습니다. 그렇지 않으면 제품에서 quantity_left
을 취합니다.
def check_quota(products):
q_list = []
for p in products:
if p.has_variations:
for v in p.variations:
q_list.append(v.quantity_left)
else:
q_list.append(p.quantity_left)
return sum(q_list)
그래서 기능 이상 0
또는 any number
중 하나를 반환합니다. 그렇다면 zero
은 매진 상품을 의미합니다.
위 코드는 정상적으로 작동하지만 목록 기능을 사용하여이 기능을 최적화하고 싶습니다.
나는 이것을 시도했다. 그러나 이것은 효과가없는 것처럼 보인다.
return sum([v.quantity_left if p.has_variations else p.quantity_left for p in products for v in i.variations])
어떻게 내부 루프에 if p.has_variations
을 적용 할 수 있습니까?
UPDATE 내가 셔츠 카테고리
[
{
"name":"full sleve",
"has_variations": True,
"variations":[
{
"type": "S size",
"quantity_left": 3
},
{
"type": "L size",
"quantity_left": 0
}
]
},
{
"name":"half sleve",
"has_variations": False,
"quantity_left": 0
},
{
"name":"sleve less",
"has_variations": False,
"quantity_left": 10
}
]
# it will return 13 means not sold out.
일부 샘플 데이터를 추가 할 수 있습니까? – cssko
조건이 충족 될 때만 루프를 사용할 수 있으므로 여기에서 목록 이해를 쉽게 사용할 수 없습니다. –
@cssko 데이터 추가 확인 업데이트 섹션. – Satendra