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

print SQL executed #71

Open
instasck opened this issue May 2, 2021 · 13 comments
Open

print SQL executed #71

instasck opened this issue May 2, 2021 · 13 comments

Comments

@instasck
Copy link

instasck commented May 2, 2021

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

@stebunovd
Copy link
Member

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.

@instasck
Copy link
Author

instasck commented May 2, 2021

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

@stebunovd
Copy link
Member

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

@instasck
Copy link
Author

instasck commented May 2, 2021

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() ?

@stebunovd
Copy link
Member

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

@instasck
Copy link
Author

instasck commented May 2, 2021

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

@stebunovd
Copy link
Member

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

@instasck
Copy link
Author

instasck commented May 2, 2021

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

@instasck
Copy link
Author

instasck commented May 2, 2021

.order_by()

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

@stebunovd
Copy link
Member

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?

@instasck
Copy link
Author

instasck commented May 2, 2021

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

@instasck
Copy link
Author

instasck commented May 2, 2021

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 ?

@stebunovd
Copy link
Member

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.

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

2 participants