问题
Lets say I have a model called Thingy, and there are 20 Thingies in my database. When I retrieve all Thingies, serializer.to_represenatation() is executed 20 times. This is good.
However, when I retrieve just a single Thingy from /api/thingies/1, I observe that serializer.to_representation() is executed four (4!!!) times.
Why does this happen, and how can I get away with just one call to to_representation()?
回答1:
That's because you are using the browsable API. JSON renderer will only call it once.
Browsable API needs several calls:
- for the result itself
- for the raw data tab when you can modify a resource through PUT
- for the raw data tab when you can modify a resource through PATCH
- for the HTML form tab
来源:https://stackoverflow.com/questions/46421437/why-does-retrieving-a-single-resource-execute-serializer-to-representation-mul