django using native SQL methods

django using native SQL methods

  1. Use extra:

    models.Book.objects.filter (publisher__name = 'fifth application legendary') .extra (WHERE = [ '. price> 50']) 
    models.Book.objects.filter (publisher__name = 'fifth application legendary' , price__gt = 50) models.Book.objects.extra (SELECT = { 'COUNT': 'SELECT COUNT (*) from hello_Book'})

  2. Use raw:


    Book.objects.raw ( 'select * from hello_Book') # Returns the model instance
  3. Execute custom SQL language:


    Connection django.db Import from Cursor = connection.cursor () # insertion cursor.execute ( "insert into hello_author (name ) values ( ' fifth application legendary')") # update the cursor.execute ( "Update hello_author the SET name = 'abc' the WHERE name = 'BCD'") # delete cursor.execute ( "the delete from hello_author the WHERE name = 'abc'") # query cursor.execute ( "select * from hello_author ") RAW = cursor.fetchone () # returns the result rows in the cursor straight forward reading, reading a cursor.fetchall () # read all

















 

Guess you like

Origin www.cnblogs.com/zuichuyouren/p/11094679.html