Django & Djson… er, JSON
Converting a DB object to JSON format
I must admin this is my first attempt at even using JSON. I wanted to produce some data that I could consume with jQuery and I figured JSON was the best format for the job.
I found Django has a method for serializing Model query sets easily and it seemed to work just fine.
The problem I was having was that I needed information from two different models. I have Envelopes that Belong to EnvelopeCategories. I wanted to return each category with all the envelopes that belonged in that category.
I kept running into errors when I would try to serialize with Django's serializer function. Basically, if I created a dictionary with what I needed, it was serialized. If I used a query set it wouldn't add the parent fields, or any additional fields I wanted to add. But finally, after browsing around the net and piecing things together I cam up with a solution:
Now, on to figuring out how to consume it for me needs. 😁