Skip to content

A suggestion for improving LogAccessMixin performance #693

@JhonatanRian

Description

@JhonatanRian

In a DetailView I configure the queryset directly in the class, like this:

class ProtocolDetailView(
    PermissionRequiredMixin, LogAccessMixin, LoginRequiredMixin, DetailView
):
    queryset = (
        Protocol.objects.select_related(
            "related_protocol",
            "department",
            "sub_department",
            "status_protocol",
            "created_by",
        )
        .prefetch_related(
            "status_protocol__history_status_protocol__doer",
            "status_protocol__history_status_protocol__department",
            "status_protocol__history_status_protocol__sub_department",
            "related_protocols__status_protocol",
            "status_protocol__history_logs__actor",
        )
        .all()
    )
    template_name = "management/protocols/detail.html"
    permission_required = ("management.view_protocol",)
    model = Protocol

Yes, a large search is done. Through the LogAccessMixin Mixin, the get_object method is called in the render_to_response method. I noticed that this causes duplicate calls in the database, that is, the get_object method has already been called previously.

The number of calls made to the bank with Mixin
image

Now I remove the Mixin and implement the render_to_response method in a similar way but fetching the value of self.object first.

def render_to_response(self, context, **response_kwargs):
    obj = self.get_object() if self.object is None else self.object
    accessed.send(obj.__class__, instance=obj)
    return super().render_to_response(context, **response_kwargs)

result:
image

I know the numbers are low, but on a larger scale it will be possible to avoid large numbers of consultations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions