Model&Form&ModelForm拾遺

Model&Form&ModelForm拾遺

1、Model&Form&ModelForm

  • Model:用於用戶請求數據的驗證(針對性弱),但有強大的數據庫操做
  • Form:強大的數據驗證(用於用戶請求數據)
  • ModelForm:強大的數據驗證,適中的數據庫操做。用於數據庫操做(只針對部分操做);用於用戶請求的驗證(只針對部分操做)

2、Model操做:

  1. 數據表操做(ORM關係對象映射)html

    Code First 建立類:自動生成表python

    DB First 建立表:自動生成類es6

    表的一對多操做:ForiegnKeysql

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

    表的多對多操做:數據庫

    第一種方式
    #建立第三張表Favor而後使用ForiegnKey上下連接
    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')
    第二種方式:比較方便(好處不少,主要是在Django的admin中會自動造成下拉框等關聯操做)
    #建立ManyToManyField關係不建立第三張表,表中不增長任何列
    #ManyToManyField還有參數
    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')
    第三種方式:使用參數關聯
    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") #正向查找使用new,反向查找使用n
        user = models.ForiegnKey('user',related_name="u")
    #正向查找是經過new 來查詢new表中的數據,反向查找是經過n 來查詢Favor表中的數據

    表的一對一操做:性能

    #注:一對一關係指的是表中數據一對一關係,而不是表的一對一關係
    #缺點:當表中列特別多的時候可能會出現錯誤
    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") #正向查找使用new,反向查找使用n
        user = models.ForiegnKey('user',related_name="u")
        #userdetail = models.ForiegnKey('UserDetail',unique=True)#至關於OneToOneField
        userdetail = models.OneToOneField('UserDetail')
    #正向查找是經過new 來查詢new表中的數據,反向查找是經過n 來查詢Favor表中的數據
    class UserDetail(models.Model):
        pwd = models.CharField(max_length=32)

    注:model的錯誤驗證和參數的用法與Form有差異fetch

    連表操做:可是會影響性能code

    ​ select_related:主動連表操做orm

    屢次sql語句而後拼接:htm

    ​ prefetch_related

  2. 參數操做

    model具體詳細請看轉載連接(此連接爲轉載,詳細瞭解請百度路飛學城):https://www.cnblogs.com/wupeiqi/articles/6216618.html

  3. 數據庫可使用同步功能,寫讀分離,寫是一個數據庫1,讀是一個數據庫2,而後把兩個數據庫同步,寫入數據時在數據庫1執行操做,讀取數據時在數據庫2進行操做。

3、Form操做:

  1. model具體詳細請看轉載連接(此連接爲轉載,詳細瞭解請百度路飛學城):https://www.cnblogs.com/wupeiqi/articles/6144178.html

4、ModelForm操做:

  1. ModelForm具體詳細請看轉載連接(此連接爲轉載,詳細瞭解請百度路飛學城):https://www.cnblogs.com/wupeiqi/articles/6229414.html

相關文章
相關標籤/搜索