Django regroup not working as expected

前端 未结 1 411
难免孤独
难免孤独 2021-01-29 00:22

I have the following view in my django application

def ViewSale( request ):
    salecur = Sale.objects.filter(user=2).order_by(\'sale_date\')
    return render_t         


        
相关标签:
1条回答
  • 2021-01-29 00:38
    {% regroup salecur by sale_date as sale_list %}
    
    <ul>
        {% for sale_date in sale_list %}
        <li>{{ sale_date.grouper }}
        <ul>
            {% for sale in sale_date.list %}
             <li>{{ sale.item }} - {{ sale.qty }} </li>
            {% endfor %}
        </ul>
        </li>
        {% endfor %}
    </ul>
    

    See the documentation on regroup:

    {% regroup %} produces a list of group objects. Each group object has two attributes:

    grouper -- the item that was grouped by (e.g., the string "Male" or "Female").
    list -- a list of all items in this group (e.g., a list of all people with gender='Male').

    0 讨论(0)
提交回复
热议问题