get_name()
call for hyperlinked relationships not required for JSON - should be lazy.
#4476
Closed
5 of 6 tasks
Labels
Milestone
Checklist
master
branch of Django REST framework.Steps to reproduce
__str__
method of the model.Expected behavior
The API should run one query to fetch the data to be serialized to JSON.
Actual behavior
The API runs one query to fetch the data to be serialized. And then it runs N queries by calling get_name on each object (which calls
__str__
, which references the related model).I suppose you could say that this is my fault for not having a 100% pure
__str__
method - but sometimes practicality beats purity.__str__
for me is primarily supposed to make it easy for me to tell what an object is when introspecting it in the shell or some other situation. I would also expect it to be called, say, when I'm in the admin looking at a list of objects and their representations.I would not expect it to be called when I'm looking at a JSON serialized version of the model that doesn't even use the results of the method anywhere. This seems like a pretty big potential pitfall that people might not even realize they're walking into.
Potential solutions:
The text was updated successfully, but these errors were encountered: