Use tuple with parentheses in Django query

Asked

Viewed 31 times

3

I made a cursor to get the menus Parent of my application, then I want to iterate each menu Parent and get the menus associated with it. However, when passing the Parent parameter, the second course does not work.

from django.db import connection
    parents=[]
    with connection.cursor() as cursor:
        cursor.execute("select parent from myapp_django_menu group by parent")
        parents=cursor.fetchall()
        print(parents)
        for p in parents:
            cursor.execute("select menu from myapp_django_menu where parent=%s group by menu",[p])
            menu=cursor.fetchone()
            print(menu)     

the parameter p, is an item of a tuple in the format [('p1',), ('p2',)] in this way, I understand that the problem should be the quotation marks, parentheses and commas. How to make this p be accepted as a parameter?

1 answer

2

I already figured out my mistake, it’s a tuple tuple, so in the for I’m taking the first tuple and I need to pass a tuple hint from the tuple from the for, it’s something like this:

...

 cursor.execute("select menu from myapp_django_menu where parent=%s group by menu",[p[0]])

...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.