<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=1,5] >[success] # 表的三種對應方式 >[info] ## 一對一 * * * * * <br> >[info] ## 一對多 -- models.ForeignKey ~~~ class Book(models.Model): name = models.CharField(max_length=32) price = models.IntegerField() pub_data = models.DateField(auto_now_add=True) 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 # 這里保存的 關聯表名_id ) print(obj) return HttpResponse('ok') ~~~ * 第二種通過對象添加,具體的操作是先查找出關聯對象,把關聯對象直接賦值給屬性 >[danger] ##### 正向查 * 正向查找通過對象包含查找 ~~~ def index(request): from cm import models obj = models.Book.objects.filter(name="python") # 因為 books 和publish 關聯,當book.publish ,是publish 庫的對象 print(obj[0].publish.city) return HttpResponse('ok') ~~~ >[danger] ##### 反向查找--反向指的是沒有ForeignKey * 通過查詢反向數據庫的對象,給到正向數據庫 ~~~ 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') ~~~ * * * * * <br> >[info] ## 多對多 -- ManyToManyFlied >[danger] ### 利用ForeignKey 創建多對多 ~~~ class HOST(models.Model): uid = models.AutoField(primary_key=True) host = models.CharField(max_length=32,db_index=True) ip = models.GenericIPAddressField(max_length=32,db_index=True) port = models.IntegerField() b = models.ForeignKey('Business',to_field = 'id') class Application(models.Model): name = models.CharField(max_length=32) class HostToApp(models.Model): hobj = models.ForeignKey('HOST',to_field='uid') aobj = models.ForeignKey('Application', to_field='id') ~~~ ![](https://box.kancloud.cn/cceb07364cdaa78f1f99c2ac28a30a8c_561x562.png) * * * * * <br> >[danger] ### 利用傳統多對多 * 一個電影有多個導演,多個導演合作導了一部電影 ~~~ class movies(models.Model): name = models.CharField(max_length=20) price = models.IntegerField() authors = models.ManyToManyField('Author') class Author(models.Model): name = models.CharField(max_length=32) ~~~ 創建出來第三個關聯表 ![](https://box.kancloud.cn/599ae96b300f0048039ee0741ec351e2_1099x403.png) * * * * * <br> >[danger] ##### 查 通過中間橋的字段可以直接插第二個表 ~~~ obj_movies = models.movies.objects.filter(id=2) obj_author = models.Author.objects.all() print(obj_movies[0].authors.values('name')) ~~~ >[danger] ##### 增加 -- add * 給一組對象 ~~~ obj_movies = models.movies.objects.create(name="大話西游", price="10") obj_author = models.Author.objects.all() obj_movies.authors.add(*obj_author) ~~~ * * * * * * 給 id ~~~ obj = [1,2] obj1 = models.movies.objects.filter(name="python") obj1[0].authors.add(*obj) ~~~ * * * * * >[danger] ##### 刪除remove 刪除和增加原理一樣 ~~~ 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>

                              哎呀哎呀视频在线观看