Reputation: 267
When serializing a django model object, the format is like this:
{
"pk":1,
"model":"book",
"fields":{
"name":"BookName",
"price":"48",
Would it be more straightforward if it's just like this:
{
"id":1,
"name":"BookName",
"price":"48",
I just wonder if there is an easy way to do this? The Json data will be sent to mobile devices such as iPhone, the client is not so interested in e.g. the name of the model, or what fields are there in the table.
A similar post Django: custom serialization options? is recommending to e.g. use template, but does that mean I need to create template for all of the models?
One more question is about full serialization, which is opposed to Django's built-in serializers that only return the related fields primary key value. I know that DjangoFullSerializers can do the trick but would it be possible to do both(customize format and full serialization)?
Upvotes: 3
Views: 2015
Reputation: 7073
from django.forms.models import model_to_dict
model_to_dict(intance, fields=[], exclude=[])
With this function you get the expected result.
Upvotes: 6