I'm one serializer inside another to show information from one model, and selected fields from another in the same response.
This looks like this:
class SimpleRouteClientConstraintSerializer(serializers.ModelSerializer):
name = serializers.SerializerMethodField()
def get_name(self, obj):
default_constraint = RouteDefaultConstraint.objects.get(pk=obj.route_default_constraint_id)
return default_constraint.name
class Meta:
model = RouteClientConstraint
fields = ('name', 'value')
class RouteSerializer(serializers.ModelSerializer):
...
# Calling first serializer here
routeClientConstraints = SimpleRouteClientConstraintSerializer(many=True, required=False)
...
class Meta:
model = Route
fields = (..., 'routeClientConstraints', ...)
depth = 1
When running the RouteSerilalizer, i want a response:
'routeClientConstraints': [{
'name': 'my name'
'value': 'confused'
},
...
]
If i use the serializer as in the code above, I get
'routeClientConstraints': [
OrderedDict([
('name', 'Minimum life boats'),
('value', '6')
])
]
I tried changing the serializer to: routeClientConstraints = SimpleRouteClientConstraintSerializer(many=True, required=False).data, which i expected would give me json format, but i got this:
'routeClientConstraints': [
OrderedDict([
('id', 1),
('value', '6'),
('created_at', '2018-11-10T14:17:39.263848Z'),
('updated_at', '2018-11-10T14:17:39.263861Z'),
('route_default_constraint', 1),
('route', 1)])
]
How do i return a list of dictionaries?