Serialize a list of objects in json in Django

In my python code in views.py, I created a list of object instances. For example, if one of my objects in models.py is Sentence, then I have a list of sentences at one point:

sentenceList = [Sentence1,Sentence2,...,SentenceN] 

I enjoyed using the template language in Django to send it to a template where I can scroll through sentences in a template, but now I am implementing some AJAX functions and would like to send something similar to this listList via serialization through the AJAX portal.

It seems that I'm between two worlds here: regular python JSON seiralizer, which serializes lists and a python object, and simplejson, which deals only with pure Django Model requests.

Does anyone have a suggestion on how I should deal with this (including not using lists of instances of objects, if for some reason this is not a good form)?

Thanks!

-Andrew

+4
source share
1 answer

A Django serializer should be able to process a regular list of Django objects, according to the docs :

(In fact, the second argument can be any iterator that gives Django objects, but will almost always be a QuerySet).

0
source

All Articles