django-pivot
django-pivot copied to clipboard
is it possible to remove doublescore for lookup columns?
for example
stock = pivot(Transaction, 'product__name',
'location__name', 'quantity', default=0)
I want response to be {'product_name' : 'Shirt'} instead of { 'product__name' : 'Shirt' }
You can easily do this in python:
# Write your own converter here
def to_camel_case(snake_str):
components = snake_str.split("__")
return components[0] + "".join(x.capitalize() if x else "_" for x in components[1:])
def sanitize_keys(data):
if isinstance(data, dict):
return {to_camel_case(k): v for k, v in data.items()}
if isinstance(data, list):
return [sanitize_keys(value) for value in data]
return data
Obviously, you will need to unpack your Query into a list - maybe this could lead to some performance issues (e.g. if you use the pivot/histogram query as a subquery)
€dit: You can simplify the sanitize_keys function if you don't want to sanitize nested keys.