[Python] Why is "connection.queries" returning an empty list in Django?

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    from django.db import connection, reset_queries

    Prints: []

    reset_queries()
    p = XModel.objects.filter(id=id) \
    .values('name') \
    .annotate(quantity=Count('p_id'))\
    .order_by('-quantity') \
    .distinct()[:int(count)]
    print(connection.queries)


    While this prints:

    reset_queries()
    tc = ZModel.objects\
    .filter(id=id, stock__gt=0) \
    .aggregate(Sum('price'))
    print(connection.queries)


    [​IMG]

    I have changed fields names to keep things simple. (Fields are of parent tables i.e. __ to multiple level)

    I was trying to print MySQL queries that Django makes and came across connection.queries, I was wondering why doesn't it prints empty with first, while with second it works fine. Although I am getting the result I expect it to. Probably the query is executed. Also am executing only one at a time.

    Continue reading...

Compartilhe esta Página