0

Good afternoon..i have a model with a class like this:

class Reportage:
    def get_num(self):
        end_date = self.date.end_date
        start_date = self.date.start_date
        duration = time.mktime(end_date.timetuple()) - time.mktime(start_date.timetuple())
        delta_t = duration / 60
        num = []

        for t in range(0,duration,delta_t):
            start = t + start_date
            end = datetime.timedelta(0,t+delta_t) + start_date
            n_num = self.get_num_in_interval(start,end)
            num.append([t, n_num])
        return num

I want to serialize with simplejson the array num [] in the views.py for passing in a second moment this array to a jquery script to plot it in a graph.. what's the code to serialize that array..? I hope I was clear .. thanks in advance to all those who respond ..

okm
  • 23,575
  • 5
  • 83
  • 90
dottorg
  • 15
  • 1
  • 4

2 Answers2

1

Following @ninefingers' response. I think that your question is aimed on how to make that dumped json string available to a jQuery plugin.

# views.py

def my_view(request):
# do stuff
num = reportage_instance.get_num()
num_json = simplejson.dumps(num)
return render(request, 'template.html', {
  'num_json': num_json,
})

In your template, you make available that json obj as a Javascript variable

# template.html

<html>
<body>
<script>
var NUM_JSON = {{num_json|safe}};
myScript.doSomething(NUM_JSON);
</script>
</body>
</html>

Now you can call regular JS with the NUM_JSON variable.

Jj.
  • 3,160
  • 25
  • 31
  • thanks for this answer, but now if i want to use the follow plugin of jqplot: "http://www.jqplot.com/tests/data-renderers.php"..what i must write in the script suggested in that website to plot my num_json array?? – dottorg May 09 '12 at 08:03
  • Reading the docs, it seems that the 2nd parameter to $.jqplot can either be a endpoint string or js data. So try $.jqplot('mychart', NUM_JSON); – Jj. May 09 '12 at 22:24
0

If you're looking to do this in a model, something like this would work:

# if this were a models.py file:

import simplejson
# other django imports:

class SomeModel(models.Model):

    property = models.SomeField()...

    def some_function(self):
        num = []

        # full num

        simplejson.dumps(num)

That'll dump num to a string-representation of the json, which you can then return, or write to a file, and so on.

From a view you have a choice - but if your view is an ajax view returning some json for processing you might do this:

# views.py

# ...

def my_ajax_call(request, model_pk):
    try:    
        mymodel = SomeModel.get(pk=model_pk)
    except SomeModel.NotFoundException as e:
        return HttpResonse("Unknown model", status=404)
    else:
        return HttpResponse(mymodel.some_function()) # returns simplejson.dumps(num)

This can then be used from a template in Javascript - the other answer shows you how you might approach that :)

  • First, thanks for the quick reply. simplejson but then I do it directly in the function of the same models.py file? now I want to use the following jquery plugin in a template, to graph the vector "num". How do I arises to change the script? http://www.jqplot.com/tests/data-renderers.php – dottorg May 08 '12 at 15:44
  • @dottorg filled my answer out a bit for you - does that help at all? –  May 08 '12 at 17:56