ivelum/djangoql

print SQL executed

Opened this issue · 13 comments

I am looking for a way to handle the SQL executed in djangoql filters.

I can change apply_search but is there an official way ? same as I do explain on QS ?

This is working:
def apply_search(queryset, search, schema=None):
"""
Applies search written in DjangoQL mini-language to given queryset
"""
ast = DjangoQLParser().parse(search)
schema = schema or DjangoQLSchema
schema_instance = schema(queryset.model)
schema_instance.validate(ast)
qs = queryset.filter(build_filter(ast, schema_instance))
print('djangoql_sql: ', qs.query)
print('djangoql_explain: ', qs.explain())
return qs

As you can see from the code snippet above, DjangoQL returns normal Django querysets, so you can use the same techniques even after its search was applied, no need to modify the library code.

Alternatively, you can take a look at common ways to debug Django queries, for example, Django Debug Toolbar is my favorite.

Where is it returned to ?
I don't see it on get_queryset of model admin.

DjangoQLSearchMixin is responsible for Django admin integration. In particular, take a look at get_search_results() method.

Where in my django admin get_search_results getting called ? is it overwrtite get_queryset ?
What if I have my own get_queryset in admin ? where should I put the explain() ?

in your admin.py (or wherever you register models in the admin):

from django.contrib import admin

from djangoql.admin import DjangoQLSearchMixin

from .models import Book


@admin.register(Book)
class BookAdmin(DjangoQLSearchMixin, admin.ModelAdmin):
    def get_search_results(self, *args, **kwargs):
        qs, use_distinct = super().get_search_results(*args, **kwargs)
        print(qs.query)
        return qs, use_distinct

Why I see "ORDER BY "instagram_data_follower"."id" DESC" on the sql statement ?
I don't need it to be ordered ...

Probably because you have ordering defined in your Django model. You can reset ordering by applying .order_by() without parameters.

BTW

@admin.register(Book)
class BookAdmin(DjangoQLSearchMixin, admin.ModelAdmin):
    def get_search_results(self, *args, **kwargs):
        qs, use_distinct = super().get_search_results(*args, **kwargs)
        if qs:
            print(qs.query)
        return qs, use_distinct

otherwise it will fail on wrong syntex

.order_by()

I found none ordering
where should I set .order_by() ? in get_queryset ?

Well, it depends on your use case where it would be appropriate to apply it. I suppose at this point we're already discussing how Django works, and not how DjangoQL works. Is there anything else related to DjangoQL I could help you with?

Well, it depends on your use case where it would be appropriate to apply it. I suppose at this point we're already discussing how Django works, and not how DjangoQL works. Is there anything else related to DjangoQL I could help you with?

well first of all thank you.
The order I found when printed the sql in djangoql
image

Might not related to QL.... I will try to find out

How can I apply a search from djangoQL using a something else:
for example I have this:
image

Filter, once I click on it I want it to execute a query via djangoql, I know how to modify it:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
But how I trigger this:
image

instead ?

Can I contribute $ to the project BTW ?

once I click on it I want it to execute a query via djangoql

Once you click on a filter option on the right, the page reloads itself. Upon page loading, Django takes the q parameter from the query string and populates the search input with its value. This is standard Django admin behavior for its built-in search. When DjangoQL is enabled, it just substitutes search internals, but it doesn't change how the search string is passed.

So if you manage to do a redirect to the same page with q parameter set to the desired search value, you should be able to achieve the behavior you described. I haven't researched how to do a redirect on list filter click, but it should be possible, I guess.

Can I contribute $ to the project BTW ?

Thanks for asking! We don't have donations configured yet, but if you check our commercial project - Teamplify, and provide some feedback for it, that would be awesome.