DEV Community

Discussion on: BUILD THE REST API USING PYTHON DJANGO - PART 4 🐍

codespresso profile image
Codespresso Author

Well, let's say we have multiple entities, each of which can be standalone entities (no foreign keys) but are referenced in some pivot tables for example. And if I had a situation where the FE sends me a request with all necessary data to create and populate multiple entities, if validation passes, we could first create entities that are not dependable, and after that, we can create all other dependencies. If for some reason it breaks, we can always roll back what we previously created. It is a bit more complex than this, but I would really like to know how do you handle N+1 problems?

Thread Thread
aminmansuri profile image
hidden_dude • Edited on

What I do is I have denormalized abstractions at the REST/Json level.

I model the API based on how the webpages/users want to use them rather than on DB normalization concepts. So that I minimize roundtrips.

Then the backend takes care of parsing the JSON and pumping it all into the correct tables.

This way the client does everything in a single transaction by sending all the form data in one single JSON (no matter how many entities may be affected).