Django study notes 2

1.BookInfo.objects.all()

  • objects: is an object of type Manager, used to interact with the database
  • When no manager is specified when defining a model class, Django provides a manager named objects for the model class
  • Managers that support explicit specification of model classes
  • When specifying a manager for a model class, django no longer generates a default manager named objects for the model class

 

2. Manager Manager

  • The manager is the interface for Django's model to query the database. Each model of the Django application has at least one manager
  • Custom manager classes are mainly used in two cases
  • Case 1: Add an extra method to the manager class: see method 2 in "Creating an object" below
  • Case 2: Modify the original queryset returned by the manager: rewrite the get_queryset() method

3. Class methods

class BookInfo(models.Model): 
omit part of the code
@classmethod def create(cls,btitle,bpub_date): b=BookInfo() b.btitle=btitle b.bpub_date=bpub_date b.bread = 0 b.bcommet = 0 b.isDelete = False return b

b=BookInfo.create('aaa','1995-1-1') #This is to directly call the method in the class, not the method of the object

 

class BookInfo(models.Model):
    omit part of the code
    def create(btitle,bpub_date):
        b=BookInfo()
        b.btitle=btitle
        b.bpub_date=bpub_date
        b.bread = 0 
        b.bcommet = 0 
        b.isDelete = False
         return b 

b=BookInfo.create('aaa','1995-1-1')# This is also possible, but the b object is created first, and then executed b.create() method

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325646290&siteId=291194637