I am relatively new to Django framework and working on getting my first application running. I am encountering the below issue when i try to pass my queryset from my view to the template.
My view.py:
with connection.cursor() as cursor:
cursor.execute("SELECT TOP(5) * FROM xxx WHERE sbu = %s", [sbu])
def dictfetchall(cursor):
columns = [col[0] for col in cursor.description]
return [dict(zip(columns,row)) for row in cursor.fetchall()]
results = dictfetchall(cursor)
class DecimalEncoder(json.JSONEncoder):
def _iterencode(self, o, markers=None):
if isinstance(o, decimal.Decimal):
return (str(o) for o in [o])
return super(DecimalEncoder, self)._iterencode(o, markers)
json_result = json.dumps(data, cls=DecimalEncoder)
I end up with the below error:
Decimal('28.80') is not JSON serializable
Any suggestions? Am i missing a step somwhere? I have lot of decimal values in the queryset.