The Django Model class manager-----------The encapsulation of database operations

Source: Internet
Author: User
Tags class manager

Model Instance Methods
    • STR (): Called when an object is converted to a string.
    • Save (): Saves the model object to the data table, and the ORM Framework is converted to the corresponding INSERT or UPDATE statement.
    • Delete (): Removes the model object from the data table and the ORM framework is converted to the corresponding DELETE statement.

properties of the model class

Property objects: Manager, an object of the manager type that is used to interact with the database.

When no manager is defined for the model class, Django generates a manager named objects for the model class, and after the custom manager, Django no longer generates the default manager objects.

The manager is an interface to the Django model for database operations, and each model of the Django application has at least one manager. Django supports custom manager classes that inherit from models. Manager.

The custom manager class is used primarily in two situations:

    • 1. Modify the original query set, overriding the All () method
    • 2. Add additional methods to the manager class, such as inserting data into the database.

1. Modify the original query set, overriding the All () method.

# Library Manager class Bookinfomanager (models. Manager):    def all        :# default query not deleted book information        # the member syntax for calling the parent class is: Super (). Method name        return super (). All (). Filter (Isdelete=false)

b) Define the manager in the model class BookInfo

class BookInfo (models. Model):    ...     = Bookinfomanager ()

2. Define methods for creating objects in the manager class

When you create a model class object, Django does not read and write to the database, calls the Save () method to interact with the database, inserts or the update operation, and saves the data to the database. If the properties of a model class are more than one property assignment is cumbersome, it is recommended to use the manager

classBookinfomanager (models. Manager): ... ..#Create a model class, receive parameters to assign a value to a property    defCreate_book (self, title, pub_date):#Create model Class object Self.model can get model classBook =Self.model () book.btitle=title Book.bpub_date=pub_date Book.bread=0 Book.bcommet=0 Book.isdelete=False#inserting data into a data tableBook.save ()returnBook

b) The books syntax for the model class BookInfo definition manager is as follows

class BookInfo (models. Model):    ...     = Bookinfomanager ()

c) The calling syntax is as follows:

Call: Book=bookinfo.books.create_book ("ABC", Date (1980,1,1))

The Django Model class manager-----------The encapsulation of database operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.