0

Why can't I convert the loop group in groupby as list? Currently, I am working on Django==2.2.1 and when I try this data = [...] below into python console, it is working fine.

from itertools import groupby
from operator import itemgetter


@login_required
def list(request, template_name='cart/list.html'):
    # I also try with this dummy data
    test_data = [{'total_order':1,'agent_name':'agentbeli','total_pcs':1,'total_kg':5.0},{'total_order':1,'agent_name':'agent123','total_pcs':1,'total_kg':5.0},{'total_order':1,'agent_name':'agent123','total_pcs':1,'total_kg':6.0},{'total_order':1,'agent_name':'agentbeli','total_pcs':1,'total_kg':6.0},{'total_order':1,'agent_name':'agentbeli','total_pcs':1,'total_kg':6.0},{'total_order':1,'agent_name':'agent123','total_pcs':1,'total_kg':7.0}]

    print(type(data)) # a list

    sorted_totals = sorted(test_data, key=itemgetter('total_order'))
    for agent_name, group in groupby(sorted_totals, key=lambda x: x['agent_name']):
        print(agent_name, list(group)) # I stopped here when converting the `group` as list.

But, I am getting an error looking like this when I try it at views in Django.

error


I also tried it with defaultdict

from collections import defaultdict


@login_required
def list(request, template_name='cart/list.html'):

    test_data = [{'total_order':1,'agent_name':'agentbeli','total_pcs':1,'total_kg':5.0},{'total_order':1,'agent_name':'agent123','total_pcs':1,'total_kg':5.0},{'total_order':1,'agent_name':'agent123','total_pcs':1,'total_kg':6.0},{'total_order':1,'agent_name':'agentbeli','total_pcs':1,'total_kg':6.0},{'total_order':1,'agent_name':'agentbeli','total_pcs':1,'total_kg':6.0},{'total_order':1,'agent_name':'agent123','total_pcs':1,'total_kg':7.0}]

    grouped = defaultdict(list)
    for data_total in test_data:
        grouped[data_total['agent_name']].append(data_total) # stoped here

    grouped_out = []
    for agent_name, group in grouped.items():
        total_order = 0
        total_pcs = 0
        total_kg = 0

        if isinstance(group, list):
            for data_total in group:
                total_order += data_total.get('total_order')
                total_pcs += data_total.get('total_pcs')
                total_kg += data_total.get('total_kg')

            grouped_out.append({
                'agent_name': agent_name,
                'total_order': total_order,
                'total_pcs': total_pcs,
                'total_kg': total_kg
            })

But the error I found stoped by wrapper view. If we following the previous issue, it referenced with this _wrapped_view

error wrapper

binpy
  • 3,994
  • 3
  • 17
  • 54

1 Answers1

0

Finally, I fixed it manually by using a dict.

test_data = [{'total_order':1,'agent_name':'agentbeli','total_pcs':1,'total_kg':5.0},{'total_order':1,'agent_name':'agent123','total_pcs':1,'total_kg':5.0},{'total_order':1,'agent_name':'agent123','total_pcs':1,'total_kg':6.0},{'total_order':1,'agent_name':'agentbeli','total_pcs':1,'total_kg':6.0},{'total_order':1,'agent_name':'agentbeli','total_pcs':1,'total_kg':6.0},{'total_order':1,'agent_name':'agent123','total_pcs':1,'total_kg':7.0}]


grouped = {}
for data_total in test_data:
    agent_name = data_total.get('agent_name')
    if agent_name in grouped:
        new_data = grouped[agent_name]  # dict
        new_data['total_order'] += data_total.get('total_order')
        new_data['total_pcs'] += data_total.get('total_pcs')
        new_data['total_kg'] += data_total.get('total_kg')

        grouped[agent_name].update(**new_data)
    else:
        grouped[agent_name] = data_total

And the result of grouped is look like this:

{'agent123': {'agent_name': 'agent123',
  'total_kg': 18.0,
  'total_order': 3,
  'total_pcs': 3},
 'agentbeli': {'agent_name': 'agentbeli',
  'total_kg': 17.0,
  'total_order': 3,
  'total_pcs': 3}}
binpy
  • 3,994
  • 3
  • 17
  • 54