Django ORM database CRUD

Django ORM database CRUD


increase

# Creation, augmentation data (recommended) 
models.UserInfo.objects.create (username = ' the root ' , password = ' 123 ' )
# Creation, augmentation data 
DIC = { ' username ' : ' Eric ' , ' password ' : ' 666 ' } 
models.UserInfo.objects.create ( ** DIC)
# Creation, augmentation data 
obj = models.UserInfo (username = ' XSK ' , password = ' 123 ' ) 
obj.save ()

delete 

# Delete the specified field line 
models.UserInfo.objects.filter (id = 4) .delete ( )
# Delete the specified fields of the plurality of rows 
models.UserInfo.objects.filter (username = ' the root ' , password = " 123 " ) .Delete ()

change

# Modify all values within the specified field becomes 888 
models.UserInfo.objects.all (). Update (password = " 888 " )

# Modify the value of the id specified in the row change field 
models.UserInfo.objects.filter (id = " . 3 " ) .Update (password = " 777 " )

check

# Get all all the data in the table () 
# Result returns QuerySet type => the Django class => [] 
# are all object UserInfo [obj (ID, username, password), obj, obj] 
Result = models.UserInfo .objects.all ()
 for Row in the Result:
     Print (row.id, row.username, row.password)
# Get the value of the object line filter username field with the root () 
Result = models.UserInfo.objects.filter (username = ' root ' )

# Get username field with a line filter root object value with the password field 123 () 
Result = models.UserInfo.objects.filter (username = ' the root ' , password = " 123 " )
 for Row in Result:
     Print (row.id, row.username, row.password)

# Get only one data 
obj = models.UserInfo.objects.first (id = nid) .first ()


#
View translated into sql statement models.UserInfo.objects.all (). Query

 

 

 

other

# .Firest () Gets the object. No output None, there is output UserInfo object (common) 
obj = models.UserInfo.objects.filter (U = username, password = P) .first ()
# .Count () Get the number is not 0 is output. 
count = models.UserInfo.objects.filter (username = u, password = p) .count ()

 

Guess you like

Origin www.cnblogs.com/xiangsikai/p/10929914.html