python3数据库表关联_Django中数据库操作|python3教程|python入门|python教程
https://www.xin3721.com/eschool/pythonxin3721/
一、數(shù)據(jù)庫(kù)中時(shí)間類(lèi)型
1.三種時(shí)間類(lèi)型:DateTimeField、DataField、TimeField三種類(lèi)型;在使用之前需先導(dǎo)入import django.utils.timezone包
2.該三種類(lèi)型分別對(duì)應(yīng)這Datetime、Data、Time三種對(duì)象;
3.時(shí)間類(lèi)型,三個(gè)屬性,auto_now_add、auto_now、default;這三種不能同時(shí)存在;auto_now_add在數(shù)據(jù)生成時(shí),填入當(dāng)前時(shí)間;
auto_now在數(shù)據(jù)時(shí)更新時(shí),時(shí)間更新;default=timezone.now默認(rèn)當(dāng)前時(shí)間;
BOOK
id
title
price
publisher(ForeignKey)
Publisher
id
name
addr
Author
id
name
sex
birthday
book(m2m)
author_book
id
author_id
book_id
二、字段參數(shù)
1.字段參數(shù):
null:表示字段是否可以為空;
unique:表示字段是否唯一;
db_index:給字段設(shè)置索引;
default:給字段設(shè)置默認(rèn)值;
2.時(shí)間字段獨(dú)有的參數(shù)
auto_now_add:生成數(shù)據(jù)時(shí)生成數(shù)據(jù);
auto_now:每次更新數(shù)據(jù)時(shí)更新數(shù)據(jù);
3.關(guān)系字段參數(shù)
to:要關(guān)聯(lián)的表
to_field:要設(shè)置關(guān)聯(lián)的字段,一般不用設(shè)置,默認(rèn)情況下為關(guān)聯(lián)另一張表里面的主鍵;
related_name:代替反向操作的 '表名_set'
反向操作時(shí),
例如:未設(shè)置該參數(shù)情況下,Publisher.objects.get(id=1).book_set.all()
設(shè)置了related_name='stu'(BOOK表里面);Publisher.objects.get(id=1).stu.all()
related_query_name:對(duì)反向操作的queryset時(shí),用來(lái)'表名_set'指定里面的?'表名'
db_constraint:取消外鍵約束,默認(rèn)為開(kāi)啟約束True;2.0版本以后需要顯示的聲明;
on_delete:對(duì)級(jí)聯(lián)操作的一些設(shè)定:(針對(duì)ForgineKey/onetoone特有),db_constraint=False;
models.CASCADE:刪除關(guān)聯(lián)數(shù)據(jù),與之相關(guān)聯(lián)的數(shù)據(jù)也一并刪除;
models.NOTHING:刪除關(guān)聯(lián)數(shù)據(jù),引發(fā)IntegrityError;
models.PROTECT:刪除關(guān)聯(lián)數(shù)據(jù),報(bào)錯(cuò)ProtectedError;
models.SET_NULL:刪除關(guān)聯(lián)數(shù)據(jù), 與之關(guān)聯(lián)的數(shù)據(jù)改為null;×為測(cè)通 報(bào)改變表結(jié)構(gòu)
models.SET_DEFAULT:刪除關(guān)聯(lián)數(shù)據(jù), 與之關(guān)聯(lián)數(shù)據(jù)改為默認(rèn)值(前提外鍵設(shè)置默認(rèn)值);
models.SET:刪除關(guān)聯(lián)數(shù)據(jù),
a. 與之關(guān)聯(lián)的值設(shè)置為指定值,設(shè)置:models.SET(值)
b. 與之關(guān)聯(lián)的值設(shè)置為可執(zhí)行對(duì)象的返回值,設(shè)置:models.SET(可執(zhí)行對(duì)象)
4.manytomany關(guān)系
manytomany表的關(guān)系維護(hù)是通過(guò)第三張表author_book來(lái)維護(hù)的;
manytomany關(guān)系涉及三張表:author表、book表、author_book表;
三張表的關(guān)系建立(主要針對(duì)第三張表author_book)三種形式:(第三張表怎么創(chuàng)建[手動(dòng)/自動(dòng)],關(guān)聯(lián)關(guān)系怎么創(chuàng)建[手動(dòng)/自動(dòng)])
方法一:Manytomany正常指定;
方法二:自己指定第三張表,通過(guò)ForeginKey來(lái)指定;自動(dòng)創(chuàng)建關(guān)聯(lián)關(guān)系
class Book(models.Model):
title = models.CharField(max_length=32, verbose_name="書(shū)名")
class Author(models.Model):
name = models.CharField(max_length=32, verbose_name="作者姓名")
# 自己創(chuàng)建第三張表,分別通過(guò)外鍵關(guān)聯(lián)書(shū)和作者
class Author2Book(models.Model):
author = models.ForeignKey(to="Author")
book = models.ForeignKey(to="Book")
class Meta:
unique_together = ("author", "book")
方法三:自己指定第三張表,并通過(guò) manytomany指定關(guān)聯(lián):手動(dòng)創(chuàng)建關(guān)聯(lián)關(guān)系;
class Book(models.Model):
title = models.CharField(max_length=32, verbose_name="書(shū)名")
#?自己創(chuàng)建第三張表,并通過(guò)ManyToManyField指定關(guān)聯(lián)
class Author(models.Model):
name = models.CharField(max_length=32, verbose_name="作者姓名")
books = models.ManyToManyField(to="Book", through="Author2Book", through_fields=("author", "book"))
# through_fields接受一個(gè)2元組('field1','field2'):
#?其中field1是定義ManyToManyField的模型外鍵的名(author),field2是關(guān)聯(lián)目標(biāo)模型(book)的外鍵名。
class Author2Book(models.Model):
author = models.ForeignKey(to="Author")
book = models.ForeignKey(to="Book")
class Meta:
unique_together = ("author", "book")
總結(jié)
以上是生活随笔為你收集整理的python3数据库表关联_Django中数据库操作|python3教程|python入门|python教程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python多线程队列和池_Python
- 下一篇: python注入_Python如何考虑代