Raw sql to json in Django, with Datetime and Decimal MySql columns
<p>I am using Ajax to make some requests from client to server, I am using DJango and I have used some Raw Sql queries before, but all of my fields was Int, varchar and a Decimal, for the last one I had an enconding problem, but I overrided the "default" property of Json and everything worked. </p>

<p>But that was before, now I have a query wich gives me Decimal and DateTime fields, both of them gave me enconding errors, the overrided "default" doesn't work now, thats why with this new one I used <code>DjangoJSONEncoder</code>, but now I have another problem, and its not an encoding one, I am using <code>dictfetchall(cursor)</code> method, recomended on Django docs, to return a dictionary from the Sql query, because <code>cursor.fetchall()</code> gives me this error: <code>'tuple' object has no attribute '_meta'</code>.</p>

<p>Before I just sended that dictionary to <code>json.dumps(response_data,default=default)</code> and everything was fine, but now for the encoding I have to use the following: <code>json.dumps(response_data,cls=DjangoJSONEncoder)</code> and if I send the dictionary in that way, I get this error:</p>

<pre><code>SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
</code></pre>

<p>And if I try to use the serializers, like this:</p>

<pre><code>response_data2= serializers.serialize('json', list(response_data))
</code></pre>

<p>And later send <code>response_data2</code> to <code>dumps</code>, I get this error:</p>

<pre><code>'dict' object has no attribute '_meta'
</code></pre>

<p>This is the code for the MySql query:</p>

<pre><code>def consulta_sql_personalizada(nombres,apellidos,puesto):
    from django.db import connection, transaction
    cursor = connection.cursor()

    cursor.execute("""select E.idEmpleado as id,CONCAT(Per.nombres_persona,' ',Per.apellidos_persona) as nombre,P.nombre_puesto as puesto,E.motivo_baja_empleado as motivo_baja,E.fecha_contratacion_empleado AS fecha_contratacion,E.fecha_baja_empleado as fecha_baja, SUM(V.total_venta) AS ventas_mes,E.fotografia_empleado as ruta_fotografia from Empleado as E
                      inner join Puesto as P on E.Puesto_idPuesto=P.idPuesto
                      inner join Venta as V on V.vendedor_venta=E.idEmpleado
                      inner join Persona as Per on E.Persona_idPersona=Per.idPersona
                      where (Per.nombres_persona like %s OR Per.apellidos_persona like %s OR E.Puesto_idPuesto=%s)
                      AND E.estado_empleado=1 AND V.estado_venta=1
                      AND
                      (YEAR(V.fecha_venta) = YEAR(Now())
                      AND MONTH(V.fecha_venta) = MONTH(Now()))""",[nombres,apellidos,puesto])
    row = dictfetchall(cursor)
    return row
</code></pre>

<p>And this is the last part of the view that makes the query and send it to ajax using json:</p>

<pre><code>        response_data=consulta_sql_personalizada(rec_nombres,rec_apellidos,rec_puesto)
        return HttpResponse(
            json.dumps(response_data,cls=DjangoJSONEncoder),
            content_type="application/json"
        )
        else:
        return HttpResponse(
            json.dumps({"nothing to see": "this isn't happening"}),
            content_type="application/json"
        )
</code></pre>

<p>What I want to know is, how can I parse the raw sql result to Json using that enconding?</p>