Loop through Product.configurableproduct.get_valid_options()

4 views
Skip to first unread message

Benjamin Hell

unread,
Feb 5, 2012, 3:19:42 AM2/5/12
to Satchmo users
Hi!

I am working on a template tag showing the inventory status for a product with two option groups (e.g. color and size) in a two-dimensional array like this:

| Small | Medium | Large
------+-------+--------+-------
red | 2 | 2 | 10
blue | 0 | 5 | 0
green | 12 | 10 | 3

My plan is to query for the relevant option groups, i.e. product.configurableproduct.option_group.select_related().all(). Then I would loop over them, basically like this:

inventory = []
for color in [c for c in colors.option_set.all()
if c in product.configurableproduct.get_valid_options()]:
sublist = []
for size in [s for s in colors.option_set.all()
if s in product.configurableproduct.get_valid_options()]:
sublist.append(product.objects.filter(SOME CRITERIA)[0].items_in_stock)
inventory.append(sublist)

That piece of code has several problems: product.configurableproduct.get_valid_options() returns option combinations and not valid options from one option group only. And then there are the criteria needed in product.objects.filter() to query a specific product variation. Do I need to make the relevant slugs and query for those, or is there a possibility to query for the "parent" product instead?

I'm stuck and would be glad about any hints!

Thanks!

Ben

Reply all
Reply to author
Forward
0 new messages