Use tuple with parentheses in query in django

3

I made a cursor to get the parent menus of my application, then I want to iterate each parent menu and get the menus associated with it. However, passing parent with 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)     

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

    
asked by anonymous 01.07.2018 / 00:24

1 answer

2

I have already discovered my error, it is a tuple of tuples, so in the case I am getting the first tuple and need to pass an index of the tuple from the for tuple, it looks something like this:

...

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

...

    
01.07.2018 / 01:52