views:

16

answers:

1

Is there a way I can serialize a FloatField model instance in django? I have the following in a management command:

def chart_data(request):
    i = 1
    chart = open_flash_chart()
    chart.title = t   
    for manager in FusionManagers.objects.all():
      net_data = manager.netio_set.values_list('Net', flat=True)
      clean = serializers.serialize('json', [ net_data, ])
      new = line()
      new.values = clean
      locals()["graph_" + str(i)] = new
      chart.add_element(locals()["graph_" + str(i)])
      i = i + 1
  return HttpResponse(chart.render())

But I get the error: 'ValuesListQuerySet' object has no attribute '_meta' The 'Net' fields are floatfields, and the values are filtered to 2 decimal places, so I get 400.23, etc... Can these be serialized?

A: 

The Django serialization module only works on lists/querysets of full Django objects; ValuesListQuerySet contains tuples, not Django objects.

I am quoting from a comment attached to Django ticket #8090. You'll need to get a QuerySet if you want to use Django's built in serialization. If not, you'll have to use a custom serializing module.

Manoj Govindan
correct! clean = list(net_data) does the trick
Ping Pengũin