Pulling out a product only once from a django view -


i'm coming across small problem pulling out list of products django view. have product page pulls through of products in model 'active' tick box ticked. can assign sub products these products.

if product has sub product assigned it, rather loading page more content load page list of sub products. @ moment in template loops through , pulls out of products. @ moment if product has 1 or more sub products assigned it, pull through each product more once, depending on how many sub products has assigned it.

the reason because i've created chain combines 2 variables in function in view. need in order sub products work.

so need limit each product appear once on template, no matter how many sub products has assigned it.

if need more info please ask! :)

*updated view answer *

view:

def producthome(request):     #prod_info= product.objects.filter(active=true, sub_product__isnull = true)     #sub_product = product.objects.filter(sub_product__isnull = false, active=true)       partner_post= partner.objects.all()      product_all = product.objects.filter(active=true)      #product_all = list(chain(prod_info, sub_product))      """     didn't need chain querysets      product_all = sorted(         chain(prod_info, sub_product),         key = attrgetter('order'), reverse=true)     """      return render_to_response('product_multi.html',{         'product_all': product_all,         'sub_product': sub_product,         'prod_info': prod_info,         'partner_post': partner_post     }, context_instance=requestcontext(request)) 

template:

{% product in product_all %}                       <li>                            <div>                         <h2 class="posttitle colortext">{{ product.name }}</h2>                         <p>{{ product.description|safe|truncatewords:13 }}...</p>                        <a href="{{ product.get_absolute_url }}" class="button">more</a>                         </div>                      </li>        {% endfor %} 

answer

turns out didn't need chain querysets pull out products together. there function doing other work. apologies confusion thanks. i've updated answer.

answer

turns out didn't need chain querysets pull out products together. there function doing other work. apologies confusion thanks. i've updated answer.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -