Model & Form & model form

Model & Form & model form

1, Model & Form & modelform

  • Model: used for user request data validation (weak pertinence), but with strong database operation
  • Form: powerful data validation (for user request data)
  • Model form: powerful data validation, moderate database operation. For database operations (partial operations only); for user requested authentication (partial operations only)

2, Model operation:

  1. Data table operations (ORM relational object mapping)

    Code First create class: auto generate table

    DB First create table: auto generate class

    One to many operation of table: ForiegnKey

    class user(models.Model):
        name = models.CharField(max_length=10)
        t = models.ForeignKey('usertype')
    class usertype(models.Model):
        name = models.CharField(max_length=10)
    

    Table many to many operations:

    The first way
    #Create the third table Favor and use the ForiegnKey up and down link
    class new(models.Model):
        title = models.CharField(max_length=10)
    class user(models.Model):
        name = models.CharField(max_length=10)
        t = models.ForiegnKey('usertype')
    class Favor(models.Model):
        new = models.ForeignKey('new')
        user = models.ForeignKey('user')
    
    The second way: more convenient (many benefits,Mainly in Django Of admin Drop down box and other associated operations will be automatically formed in)
    #Creating a ManyToManyField relationship does not create a third table, and no columns are added to the table
    #ManyToManyField also has parameters
    class new(models.Model):
        title = models.CharField(max_length=10)
        favor = models.ManyToManyField('user')
    class user(models.Model):
        name = models.CharField(max_length=10)
        t = models.ForeignKey('usertype')
    
    Third way: use parameter association
    class new(models.Model):
        title = models.CharField(max_length=10)
        favor = models.ManyToMany('user',
                                  through="Favor",through_fields=("new","user"))
    class user(models.Model):
        name = models.CharField(max_length=10)
        t = models.ForiegnKey('usertype')
    class Favor(models.Model):
        new = models.ForiegnKey('new',related_name="n") #Forward lookup uses new, reverse lookup uses n
        user = models.ForiegnKey('user',related_name="u")
    #Forward lookup queries the data in the new table through new, and reverse lookup queries the data in the Favor table through n
    

    One to one operation of table:

    #Note: the one-to-one relationship refers to the one-to-one relationship of data in the table, rather than the one-to-one relationship of the table
    #Disadvantage: errors may occur when there are too many columns in the table
    class new(models.Model):
        title = models.CharField(max_length=10)
        favor = models.ManyToMany('user',
                                  through="Favor",through_fields=("new","user"))
    class user(models.Model):
        name = models.CharField(max_length=10)
        t = models.ForiegnKey('usertype')
    class Favor(models.Model):
        new = models.ForiegnKey('new',related_name="n") #Forward lookup uses new, reverse lookup uses n
        user = models.ForiegnKey('user',related_name="u")
        #userdetail = models.ForiegnKey('UserDetail',unique=True)#Equivalent to OneToOneField
        userdetail = models.OneToOneField('UserDetail')
    #Forward lookup queries the data in the new table through new, and reverse lookup queries the data in the Favor table through n
    class UserDetail(models.Model):
        pwd = models.CharField(max_length=32)
    

    Note: the error validation and parameter usage of model are different from that of Form

    Connect table operation: but it will affect performance

    Select [related: active connect table operation

    Multiple sql statements are spliced:

    ‚Äč prefetch_related

  2. Parameter operation

    For details of the model, please refer to the reprint link (this link is reprint, for details, please refer to Baidu Lu Feixue city): https://www.cnblogs.com/wupeiqi/articles/6216618.html

  3. The database can use the synchronization function to separate the write and read. The write is a database 1 and the read is a database 2. Then synchronize the two databases. When writing data, perform the operation in the database 1, and when reading data, perform the operation in the database 2.

3, Form operation:

  1. For details of the model, please refer to the reprint link (this link is reprint, for details, please refer to Baidu Lu Feixue city): https://www.cnblogs.com/wupeiqi/articles/6144178.html

4, ModelForm action:

  1. For details of ModelForm, please refer to the reprint link (this link is reprint, for details, please refer to Baidu lufeixuecheng): https://www.cnblogs.com/wupeiqi/articles/6229414.html

78 original articles published, 8 praised, 845 visited
Private letter follow

Tags: Database Django SQL

Posted on Thu, 16 Jan 2020 01:34:29 -0500 by 3.grosz