Refactor: Add list_select_related for performance improvement #810
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request aims to enhance the performance of the djangorestframework-simplejwt by optimizing database queries in the admin interface. By strategically using the
list_select_related
attribute in the admin classesOutstandingTokenAdmin
andBlacklistedTokenAdmin
, we significantly reduce the number of database hits required to render the admin list pages.Changes Made:
list_select_related = ("user",)
to select related user objects efficiently.list_select_related = ("token__user",)
to select related token user objects, improving performance.Motivation:
In the current implementation, the admin list pages execute separate database queries for each related object displayed in the list. This approach becomes inefficient when dealing with large datasets, leading to performance bottlenecks. By explicitly specifying the related fields to be included in the query results, we eliminate the need for additional database hits, resulting in a notable performance improvement.
Impact:
list_select_related
attributes adhere to best practices and contribute to cleaner, more efficient code.Testing:
All existing tests have been run to ensure that the functionality remains intact after the optimization. Additionally, manual testing has been performed to validate the improved performance in real-world scenarios.
Future Considerations:
Continued monitoring and profiling of performance metrics will be beneficial to gauge the effectiveness of this optimization over time. Further refinements may be made based on feedback and performance analysis.