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
Post a Comment