對Django外來鍵關係的描述

2022-10-04 16:42:13 字數 3453 閱讀 2418

注:本文需要你有一定的資料庫知識,本文的資料庫語法使用mysql書寫

django中,跟外來鍵有關的關係有三種,下面來一一介紹。

onetomanyfield

這種最好理解,說白了就是最普通的外來鍵,看看下面兩個模型:

class goodstype(models.model):

name = models.charfield(max_length=50)

class goodsmessage(models.model):

title = models.charfield(max_length='100') # 商品標題

category = models.manytomanyfield(goodstype) # 商品標籤

分析一下:

這裡django會在資料庫中創兩張表:

create table goodstype(

`id` int(11) not null auto_increment,

`name` varchar(50) not null,

primary key (`id`)

)create table goodsmessage(

`id` int(11) not null auto_increment,

`title` varchar(50) not null,

`category_id` int(11) not null,

primary key (`id`),

foreign key (`category_id`) references `schoolbuy_goodstype` (`id`)

)這樣的結果就是乙個商品會對應乙個類別,即類別是商品的外來鍵。

onetoonefield

這種關係和onetomany類似,是一種有約束的外來鍵,看看下面兩個模型:

class goodstype(models.model):

name = models.charfield(max_length=50)

class goodsmessage(models.model):

title = models.charfield(max_length=www.cppcns.com'100') # 商品標題

category = models.onetomanyfield(goodstype) # 商品標籤 (變為一對一關係)

他們會使得資料庫建立什麼表呢?

create table goodstype(

`id` int(11) not null auto_increment,

`name` varchar(50) not null,

primary key (`id`)

)create table goodsmessage(

`id` int(11) not null auto_increment,

`titlewww.cppcns.com` varchar(50) not null,

`category_id` int(11) not程式設計客棧 null,

primary key (`id`),

foreign key (`category_id`) references `schoolbuy_goodstype` (`id`),

unique key `schoolbuy_goodsmessage_category_id_4dd415fc1e19cf24_uniq` (`category_id`) # 新增

)那麼這裡已經很明顯了,在這兩個模型裡,每個商品有乙個商品型別,並且每個商品型別只屬於乙個商品(用了unique約束),即如果我a商品的型別是電腦,那麼其他商品的型別都不能定義為電腦了。

所以商品與型別的對應關係肯定不能是onetoone,而應該是onetomany。

那麼onetoone用在**呢?這裡說乙個地方,在擴充套件django的user模型時,因為系統自帶的字段不夠,所以一種最基本的擴充套件方法是定義乙個user_profile表,用來作為使用者的擴充套件,那麼一條使用者記錄只會有乙個擴充套件表記錄,並且這個這個記錄也只屬於該使用者。

manytomany

多對多關係,這裡我們假設一種情景:

我現在有乙個商品表,這個商品有一些(不定數量),那rqipbcxljg麼可以使用多對多關係:

class goodspicture(models.moderqipbcxljgl):

pic = models.imagefield(upload_to='pic/')

class goodsmessage(models.model):

title = models.charfield(max_length='100') # 商品標題

pic = models.manytomanyfield(goodspicture)

這裡資料庫不同啦,建立了三張表,具體如下:

create table goodspicture(

`id` int(11) not null auto_increment,

`pic` varchar(255) not null, # django對於的儲存採用的是二進位制檔案存硬碟,資料庫只儲存路徑

primary key (`id`)

)create table goodsmessage(

`id` int(11) not null auto_increment,

`title` varchar(50) not null,

primary key (`id`)

# 注意了,這裡沒有外來鍵約束了

)create table goodsmessage_coodspicture(

`id` int(11) not null auto_increment,

`goodsmessage_id` int(11) not null,

`goodpicture_id` int(11) not null,

primary key (`id`),

unique key `goodsmessage_id` (`goodsmessage_id`,`goodspicture_id`),

foreign key (`goodsmessage_id`) references `goodsmessage` (`id`),

foreign key (`goodstype_id`) references `goodspicture` (`id`)

)前兩個表就不講了,主要說一下第三個表goodsmessage_coodspicture,

django用這個表來記錄一條資料,內容為:某個商品對應某張。其中有乙個unique約束,說明不能有重複的記錄。

這樣,每次查詢goodsmessage_coodspicture表,就能獲得某件商品對應的。

這裡講了他們在資料庫中的實現,那麼django如何來查詢這些資料呢,有一篇好的博文推薦給大家:

基於django orm、一對

一、一對多、多對多的全面講解

本文標題: 對django外來鍵關係的描述

本文位址:

Django外來鍵關係描述

注 本文需要你有一定的資料庫知識,本文的資料庫語法使用mysql書寫 django中,跟外來鍵有關的關係有三種,下面來一一介紹。這種最好理解,說白了就是最普通的外來鍵,看看下面兩個模型 class goodstype models.model name models.charfield max le...

MySql的關係(外來鍵)

比如 建立成績表scores,結構如下 1.建立關係 1.1外來鍵約束 有二種 1.1.1建立表時可以直接建立約束 create table scores id int primary key auto increment,stuid int,subid int,score decimal 5,2 ...

django模型中有外來鍵關係的表刪除相關設定

例如有author authordetail兩表 author models.onetoonefield to author null true,related name detail db constraint false,on delete models.cascade 1 關係字段放在auth...