python-django的ORM

來源:互聯網
上載者:User

標籤:資料庫   root   object   關聯表   主表   ted   sse   tom   student   

  • 類代表資料庫表
  • 類的對象代指資料庫的一行記錄
  • FK欄位代指關聯表中的一行資料
  • ManyToMany欄位,自動產生第三張表:依賴關聯表對第三張表間接操作
  • 正向:FK欄位;反向:小寫類名)_set =>releated_name=‘ssss‘
  • 誰是主表?就列出其全部資料
    • models.Student.object.all().values(‘useename‘,‘cs__title‘)
    • models.Classes.object.all().values(‘tittle‘,‘ssss__username

樣本:
- 所有學生的姓名以及其所在班級名稱,QuerySet
stu_list = Student.objects.all()
select * from tb;
[obj,obj,obj,obj]

stu_list = Student.objects.all().values("id",‘username‘)
select id,username from tb;
[{"id":1,‘username‘:‘xx‘},{id:‘‘,username:‘‘}]

stu_list = Student.objects.all().values_list("id",‘username‘)
[(1,‘root‘), (2,‘alex‘)]


stu_list = Student.objects.all().values(‘username‘,"cs__name")
for row in stu_list:
print(row[‘username‘],row[‘cs__name‘])

stu_list = Student.objects.all().values(‘username‘,"cs__titile",“cs__fk__name”)

python-django的ORM

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.