django - Convert a flat model into a structured one -


basically, want able convert like

{     "name": "",     "street_address": "",     "city": "" } 

into this

{     "name": "",     "address" : {         "street_address": "",         "city": ""     } } 

while still retaining model this

class modela(models.model):     name = models.charfield()     street_address = models.charfield()     city = models.charfield() 

any ideas?

override to_representation() method alter serialization.

class modelaserializer(serializers.modelserializer):     class meta:         model = modela         fields = ('name', 'street_address', 'city')       def to_representation(self, instance):         x = super(modelaserializer, self).to_representation(instance)         desired_response = {}         desired_response['name'] = x['name']         desired_response['address'] = {'street_address': x['street_address'],                                        'city': x['city']}         return desired_response 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -