The basic model of the database operations 13.ORM

 

adding data:

As long as you create an object model and then use ORM ORM model call this savemethod can save the
sample code as follows:

book = Book(name='西游记',author='吴承恩',price=100)
book.save()

Find data

All work is used to find the model objectsattributes to complete the course, the query object can customize the function of this part will be mentioned later...
1. The primary lookup key: the primary key may be used to find objects.geta method of and. transmission pk=xxmode to find the following sample code:

    book = Book.objects.get(pk=2)


2. The other fields Find: use objects.filtermethod to find the following sample code:

    books = Book.objects.filter(name='三国演义')


Using the filtermethod is a return to QuerySetthe object. This object is similar to the list. We can use this object firstmethods to get the first value.

delete data:

First, find the corresponding data model. Then execute this model deletemethod to delete. Sample code is as follows:

book = Book.objects.get(pk=1)
book.delete()

change the data:

First, find the corresponding data model. Then modify the value of the property on the model. Then execute savemethod to complete the modification. Sample code is as follows:

    book = Book.objects.get(pk=2)
    book.price = 200
    book.save()

Guess you like

Origin www.cnblogs.com/ys-python/p/11266156.html