I am trying to make a livesearch with Django and jQuery. What I’ve done is make the javascript ask for some data with the getJSON function, then I’ve set up a view in Django that returns a JSON response automated by the Django serializer.
And this works well, it returns a json response with the text/javascript content-type. To avoid sending all the data, (alot that i dont need) i did this:
response.write(serializers.serialize('json', soknad_list, fields=('name', 'image', 'genre')))
But for instance, the ‘genre’ field is a manyToMany field, so is it possible to get the values from genre.all.0 for instance and not just the genre ID?
And the model has a function get_absolute _url, is it possible to include this in the JSON response, if so how?
So i guess my question is, is it possible to include stuff other than the raw field data in the JSON response, if not, how would you go about solving my problem?
I found out that the simplest thing was to not use the serializer at all. I dont know why I didnt think of this before, but i just used a generic object list view and changed the mimetype to text/javascript and made a JSON template insted of a html template.
Very simple, and that way i managed to get all the data i wanted into the JSON response. This way you can add everything that you can add to a html template into a JSON response, even paginating.
Example extraction of the view i created: