I'm doing a simple query:
q = `Select * from table`
Table.objects.raw(q)
which will give me RawQuerySet
.
Is there a way to get the result in the form of tuple of tuples ? eg If there would have been just 2 fields in the table, then the result would look like:
((1, 'name1'), (2, name2))
Any reason not to do it in Python?
rqs = Table.objects.raw(q)
tuples = tuple((o.pk, o.name) for o in rqs)
If you don't want to return model instances, then I don't see any advantage in using raw()
. You can execute custom SQL directly instead:
from django.db import connection
with connection.cursor() as cursor:
cursor.execute('SELECT foo FROM bar WHERE baz = %s', [self.baz])
result = cursor.fetchall()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.