Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A suggestion for improving LogAccessMixin performance #693

Open
JhonatanRian opened this issue Dec 28, 2024 · 0 comments
Open

A suggestion for improving LogAccessMixin performance #693

JhonatanRian opened this issue Dec 28, 2024 · 0 comments

Comments

@JhonatanRian
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant