17. CRUD operations

 

Model of operation:

In the ORM framework, all models related operations, such as add / delete. In fact, it is mapped to a database of operational data. Thus the model operation is operation data in a database table.

Adding a model to the database:

Adding to the current database. First you need to create a model. To create the model is very simple, just creating a normal Python objects is exactly the same. After creating the model, we need to call the save method of the model, so that Django will automatically convert this model into sql statement, and then stored in the database. Sample code is as follows:

class Book(models.Model):
    name = models.CharField(max_length=20,null=False)
    desc = models.CharField(max_length=100,name='description',db_column="description1")
    pub_date = models.DateTimeField(auto_now_add=True)

book = Book(name='三国演义',desc='三国英雄!')
book.save()

Find data:

Find data is through the next model objectsto achieve the target.

Find all the data:
To find all the data in the Book model corresponding table. Then the following sample code:

books = Book.objects.all()


Above will return all the data in the Book model.

Data filtering:

Find data in time, and sometimes need some data to filter. So this time we need to call objectsthe filtermethod. Example code:

books = Book.objects.filter(name='三国演义')
> [<Book:三国演义>]

# 多个条件
books = Book.objects.filter(name='三国演义',desc='test')


Calls filter, will meet the conditions of all the model objects are returned.

Gets a single object:

Use filterreturns the result set to satisfy all the conditions. Sometimes if you only need to return the first object to meet the conditions. You can use the get method. Sample code is as follows:

book = Book.objects.get(name='三国演义')
>> <Book:三国演义>


Of course, if the object is not found to meet the criteria, it will throw an exception. The filter in the absence of data to find the time to meet the conditions, return an empty list.

Data Sort:

In the previous example, the data is disordered. If you want to use a field in the data to find the time to sort, you can use order_by methods. Sample code is as follows:

books = Book.objects.order_by("pub_date")


In the above code to extract data when all the books will be used pub_datefrom small to large sort. If you want to reverse sort, you can in front of pub_date a minus sign. Example code:

books = Book.objects.order_by("-pub_date")

change the data:

After finding the data, it can be modified. Modification is very simple, just to find out a property of the object to be modified, and then call the save method this object can be modified. Sample code is as follows:

from datetime import datetime
book = Book.objects.get(name='三国演义')
book.pub_date = datetime.now()
book.save()

delete data:

After finding the data, it can be deleted. Delete data is very simple, just call the delete method to this object. Example code:

book = Book.objects.get(name='三国演义')
book.delete()

Guess you like

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