代碼目錄結構
|-- djago_study
|-- manage.py
|-- test_django.py
|-- app01
| |-- admin.py
| |-- apps.py
| |-- models.py
| |-- tests.py
| |-- views.py
| |-- init.py
| |-- migrations
| |-- 0001_initial.py
| |-- init.py
|-- djago_study
| |-- settings.py
| |-- urls.py
| |-- wsgi.py
| |-- init.py
|-- templates
數據表
表 app01_author
id | name | age | phone | detail_id |
---|---|---|---|---|
1 | 格非 | 20 | 1890000000 | 1 |
2 | 邁克爾·湯普森 | 22 | 1310000000 | 2 |
表 app01_author2book
id | author_id | book_id |
---|---|---|
1 | 1 | 1 |
2 | 2 | 2 |
表 app01_authordetail
id | hobby | addr |
---|---|---|
1 | 踢毽子 | 北京 |
2 | 看書 | 上海 |
3 | 逛街 | 廣州 |
表 app01_book
id | title | publish_date | price | publisher_id |
---|---|---|---|---|
1 | 沙河異聞錄 | 2019-09-29 | 100.00 | 1 |
2 | 養育男孩 | 2019-09-03 | 80.00 | 2 |
3 | 沙河三部曲 | 2018-10-24 | 70.00 | 2 |
表 app01_publisher
id | name | city |
---|---|---|
1 | 北京十月文藝出版社 | 北京 |
2 | 中信出版社 | 北京 |
表 django_migrations
id | app | name | applied |
---|---|---|---|
1 | app01 | 0001_initial | 2019-09-29 07:49:04.899704 |
./test_django.py
import os
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djago_study.settings')
import django
django.setup()
from app01 import models
ret = models.Book.objects.filter(title__contains="沙河")
print("查找包含沙河的書", ret)
ret = models.Book.objects.filter(publish_date__year=2018)
print("查找出版日期2018的書", ret)
ret = models.Book.objects.filter(price__gt=10)
print("查找價格大於10的書", ret)
ret = models.Publisher.objects.filter(city__contains="北京")
print("查找在北京的出版社", ret)
ret = models.Book.objects.all().values_list("publisher__name")
print("查所有書關聯的出版社", ret, "去重後的結果集", ret.distinct()) # distinct 去重
ret = models.Book.objects.all().order_by("price").reverse()
print("講所有書,按照價格排序 方法一", ret)
ret = models.Book.objects.all().order_by("-price").reverse()
print("講所有書,按照價格排序 方法二", ret) # - 負號表示倒序
ret = models.Book.objects.filter(title="沙河異聞錄").values("publisher__city")
print("查找書籍沙河異聞錄出版社的所在的城市", ret)
ret = models.Book.objects.filter(title="沙河異聞錄").values("author__detail__hobby")
print("查找書籍是沙河異聞錄作者的愛好", ret)
if __name__ == '__main__':
main()
./app01/models.py
# Create your models here.
from django.db import models
# 出版社
class Publisher(models.Model):
name = models.CharField(max_length=32)
city = models.CharField(max_length=32)
def __str__(self):
return self.name
# 書
class Book(models.Model):
title = models.CharField(max_length=32)
publish_date = models.DateField(auto_now_add=True)
price = models.DecimalField(max_digits=5, decimal_places=2)
# 創建外鍵,關聯publish
publisher = models.ForeignKey(to="Publisher", on_delete=models.CASCADE)
def __str__(self):
return "書名 {} 價格 {}".format(self.title, self.price)
# 作者
class Author(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField()
phone = models.IntegerField()
# 通過through指定第三張表
# through_fields指定表字段
books = models.ManyToManyField(to="Book", through="Author2Book", through_fields=("author", "book"))
detail = models.OneToOneField(to="AuthorDetail", on_delete=models.CASCADE)
def __str__(self):
return self.name
# 自己動手 創建作者和書關聯的第三張表
# 此時 在ORM層面
class Author2Book(models.Model):
id = models.AutoField(primary_key=True)
# 作者id
author = models.ForeignKey(to="Author", on_delete=models.CASCADE)
# 書id
book = models.ForeignKey(to="Book", on_delete=models.CASCADE)
class Meta:
# 建立唯一約束, 保證不可重複
unique_together = ("author", "book")
# 作者詳情
class AuthorDetail(models.Model):
# 愛好
hobby = models.CharField(max_length=32)
# 地址
addr = models.CharField(max_length=128)
整套源碼下載地址
https://www.lanzous.com/i6hew4d
相關資料
F查詢操作
https://docs.djangoproject.com/en/2.2/ref/models/expressions/#django.db.models.F
雙下劃線方法操作
https://www.dev2qa.com/what-does-double-underscore-__-means-in-django-model-queryset-objects-filter-method/
Q查詢操作
https://docs.djangoproject.com/en/2.2/ref/models/querysets/#q-objects
聚合查詢示例
https://docs.djangoproject.com/en/2.2/topics/db/aggregation/
查詢教程
https://docs.djangoproject.com/en/2.2/topics/db/queries/
Model字段 AutoField BigIntegerField
https://docs.djangoproject.com/zh-hans/2.2/ref/models/fields/#decimalfield
ORM那些相關操作
https://www.cnblogs.com/liwenzhou/p/8660826.html