Skip to content
Advertisement

How can I optimze Django Code to only one query request

I have a simple django project which displays previous reservations dates based on id’s. However currently there are 2 requests being made. (N+1 sql requests, where N is the reservation’s count) Do you have any idea how i would be able to optimize this code to only 1 query?

This is the model.py file

JavaScript

This is the view.py file -> Where the queries are being made

JavaScript

The get_reservation function in the model is being called in the template file here:

JavaScript

Advertisement

Answer

My only thought is to add it as part of the first request and remove get_previous_reservation from being called. Pseudocode-ish:

JavaScript

My point is you need to do it after you make the first query (my code above is only approximate, you may want to use a custom class instead of a dict like I did. Hope it helps.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement