Home > database >  How to show 10 product in django template by using _set(reverse lookup class)?
How to show 10 product in django template by using _set(reverse lookup class)?

Time:12-22

{% for item in i.product_set.all %}                
                <div >
                    <div >
                        <span >
                            <img width="80" height="80" src="{{ item.image.url }}" alt="">
                        </span>
                        <h5>{{ item.name }} <span>৳ {% if item.discount %} {{ item.price|sub:item.discount }} {% else %}  {{ item.price }} {%endif%}</span></h5>
                        <p>{{ item.description|truncatewords:6 }}</p>
                    </div>
                </div>
                    {% endfor %}  

Above code show all product but I want to show last 10.

CodePudding user response:

You can try something like this taking last user product IDs in views.

last_ten_product = product.objects.filter(since=since).order_by('-product_id')[:10]

CodePudding user response:

just create a method in your model and call it in template

self.product_set.all()[0:10] 
  • Related