Skip to content

API Reference¤

It is responsible for displaying the data sent by ClassifiedQuery.
By adding customized settings, processed data can be processed one more time to make the necessary output.

BasePresenter¤

You can inherit this class to create a new Presenter.
All you have to do is override the print method.
ClassifiedQuery It has only one attribute.
Everything has a type hint.
If you look at the example below how you implemented the class, you may have an idea.

How to Customize Presenter¤

Let's define a presenter that simply outputs the location of the query that occurs the most.

Create a class that inherits the BasePresenter.¤

from django_query_capture.presenter import BasePresenter


class MostQueryLocationPresenter(BasePresenter):
    pass

Refer to the property of ClassifiedQuery to get the most common similar.¤

Refined properties refer to ClassifiedQuery, and individual properties such as sql and duration refer to ClassifiedQuery's CapturedQuery.

from django_query_capture.presenter import BasePresenter


class MostSimilarQueryLocationPresenter(BasePresenter):
  def print(self):
    captured_query, counter = self.classified_query['most_common_similar'][0]  # Get the counters of the most duplicated items. (Refer to typehint).
    if captured_query:
        print(f'[{captured_query["function_name"]}, {captured_query["file_name"]}:{captured_query["line_no"]}], duplicates {counter} times.')

Import the Presenter defined in Settings.¤

# settings.py

QUERY_CAPTURE = {
  ...,
  "PRESENTER": "some.path.MostQuerySimilarLocationPresenter",
}

It's done!¤

from django_query_capture import  query_capture

from news.models import Reporter

@query_capture()
def some_view():
    for reporter in Reporter.objects.all():
        for article in reporter.article_set.all():
            # ...

# Output
# [some_function, some_path.py:50], similar 20 times.

Example¤

simple_query_presenter.png

raw_line_presenter.png

pretty_presenter.png

only_slow_query_presenter.png


Last update: January 18, 2022