Resolve #494: Reduce duplicate and unnecessary database requests #853
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.
Proposed changes in this pull request
select_related()
,prefetch_related()
, andannotate()
) to combine a list query and the subsequent individual list item queries into just 1 database access whenever possible. (The attachment count of resources is not optimized because it seems aggregation is impossible (?) due toContentObject
’s generic relations.)only()
ordefer()
to only retrieve relevant fields or to avoid retrieving theattributes
field when it is not needed. Also addget_queryset()
managers for theSpatialUnit
,Party
, andTenureRelationship
to defer the models’attributes
field by default. Note:SpatialRelationshipManager
was moved from models.py to a new managers.py file.CacheObjectMixin
for views that do not have an explicitget_object()
method. And switch a single-object view from usingget_queryset()
to instead declaring the model and leveragingCacheObjectMixin
.SpatialQuerySetMixin.get_queryset()
to either return all project locations or all excluding the current location for DRY purposes.is_administrator()
methods ofOrgAdminCheckMixin
andProjectAdminCheckMixin
:SuperUserCheckMixin
instead of having the superuser check code again.try
ing to search for their OA or PM roles inOrganizationRole
andProjectRole
instead of constructing a list of org admins or project admins and then checking if the user is in the list.SuperUserCheckMixin
,OrgAdminCheckMixin
, andProjectAdminCheckMixin
as needed instead of having repeated code.get_context_data()
declarations or code if they can be inherited from a mixin.reverse()
calls in views to reuse already available URLkwargs
instead of retrieving organization and project objects.refresh_from_db()
to retrieving a fresh instance from the database.super()
calls to Python 3.Limitations
When should this PR be merged
For Sprint 10.
Risks
None foreseen. The updated code should be functionally the same to the current code (except for the username/full name switch-around in the resources widget).
Follow up actions
None. (But, resolving #572 can further cut down on database requests but that issue needs discussion.)