Sql Raw queries in django

There two types of raw queries in djnago :

1.perform raw queries and return model instances
2.execute custom SQL directly.


Example for 1st procedure.

In MySQL:

select  *  from  book_product

In Django:

from book.models import Product

p = Product.objects.raw('select  *  from book_product')

*Note here book is the app name of the project.



Example for 2nd procedure.

from django.db import connection
 
cursor = connection.cursor()
 
cursor.execute('select * from book_product')
 
row = cursor.fetchall()
      or  
row = cursor.fetchone()
 
 
Ref:Click here 
 




No comments:

Post a Comment