<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] >[success] # 一、一對多 利用ForeignKey 創建表 注關聯表中會自動添加字段后面加_id,相當于publish _id,其中單獨的publish包含整個Publish對象 ~~~ class Book(models.Model): name = models.CharField(max_length=20) price = models.IntegerField() pud_data = models.DateField() publish = models.ForeignKey('Publish') class Publish(models.Model): name = models.CharField(max_length=32) city = models.CharField(max_length=32) ~~~ >[danger] ##### 增 第一種方法通過真實的字段,也就是在_id添加 ~~~ def index(request): from cm import models obj = models.Book.objects.create( name = "c#", price = 200, pud_data = '2008-08-08', publish_id= 1 ) print(obj) return HttpResponse('ok') ~~~ 第二種通過對象添加,具體的操作是先查找出關聯對象,把關聯對象直接賦值給屬性 查 正向查找通過對象包含查找 ~~~ def index(request): from cm import models obj = models.Book.objects.filter(name="python") print(obj[0].publish.city) return HttpResponse('ok') ~~~ * 反向查找,通過關聯對象查找 第一種通過對象反差 ~~~ def index(request): from cm import models obj = models.Publish.objects.filter(name="理工")[0] obj1 = models.Book.objects.filter(publish=obj).values("name") print(obj1) return HttpResponse('ok') ~~~ 第二種通過set反查,通過關聯的類后面加_set ~~~ def index(request): from cm import models obj = models.Publish.objects.filter(name="理工")[0] obj1 = obj.book_set.all() print(obj1) return HttpResponse('ok') ~~~ 第三種直接屬性雙下滑下接字段,例如publish__name ~~~ def index(request): from cm import models obj = models.Book.objects.filter(publish__name="理工").values("name") print(obj) return HttpResponse('ok') ~~~ >[success] # 二、多對多 利用ManyToManyField創建表 ~~~ class Book(models.Model): name = models.CharField(max_length=20) price = models.IntegerField() pud_data = models.DateField() authors = models.ManyToManyField('Author') class Author(models.Model): name = models.CharField(max_length=32) ~~~ >[danger] ##### 查 利用查詢包含ManyToManyField對象,進行查找關聯表 ~~~ def index(request): from cm import models obj = models.Book.objects.filter(name="go") print(obj[0].authors.all().values('name')) return HttpResponse('ok') ~~~ 增加add,先找到關聯對象,在有add添加對象 ~~~ def index(request): from cm import models obj = models.Author.objects.all() obj1 = models.Book.objects.filter(name="python") obj1[0].authors.add(*obj) return HttpResponse('ok') ~~~ 或者直接添加主鍵 ~~~ def index(request): from cm import models obj = [1,2] obj1 = models.Book.objects.filter(name="python") obj1[0].authors.add(*obj) return HttpResponse('ok') ~~~ remove刪除 ~~~ def index(request): from cm import models obj = [1,] obj1 = models.Book.objects.filter(name="python") obj1[0].authors.remove(*obj) return HttpResponse('ok') ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看