Note: This article requires you to have some knowledge of databases, the database syntax of this article written using mysql
There are three kinds of relationships related to foreign keys in Django, which are described below.
OneToManyField
This is best understood as the most common foreign key, to put it bluntly, look at the following two models:
class GoodsType(): name = (max_length=50) class GoodsMessage(): Title = (max_length='100') # Product Title Category = (GoodsType) # Commodity labels
Analyze it:
Here Django creates two tables in the database:
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`) )
The result of this is that a product will correspond to a category, i.e. the category is a foreign key to the product.
OneToOneField
This relationship is similar to OneToMany, a constrained foreign key, look at the two models below:
class GoodsType(): name = (max_length=50) class GoodsMessage(): Title = (max_length='100') # Product Title Category = (GoodsType) # Commodity labels (changed to one-to-one relationship)
What tables will they cause the database to create?
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`), UNIQUE KEY `SchoolBuy_goodsmessage_Category_id_4dd415fc1e19cf24_uniq` (`Category_id`) # New )
So it's already obvious here that in both models, there is one commodity type per commodity and each commodity type belongs to only one commodity (with UNIQUE constraints), i.e., if I have a type of computer for commodity A, then no other commodity type can be defined as a computer.
So the correspondence between goods and types must not be OneToOne, but OneToMany.
So where is OneToOne used? Here is a place to say, in the extension of Django's User model, because the system comes with not enough fields, so one of the most basic extension method is to define a User_profile table, used as a user's extension, then a user record will only have an extension table records, and this record also belongs to the user only.
ManyToMany
Many-to-many relationship, here we assume a scenario:
I now have a table of products and this product has some images (variable number), then a many-to-many relationship can be used:
class GoodsPicture(): Pic = (upload_to='pic/') class GoodsMessage(): Title = (max_length='100') # Product Title Pic = (GoodsPicture)
Here the database is different, the establishment of three tables, as follows:
create table GoodsPicture( `id` int(11) NOT NULL AUTO_INCREMENT, `Pic` varchar(255) NOT NULL, # Django for the preservation of images using a binary image file stored on the hard disk, the database only saves the path to the image PRIMARY KEY (`id`) ) create table GoodsMessage( `id` int(11) NOT NULL AUTO_INCREMENT, `Title` varchar(50) NOT NULL, PRIMARY KEY (`id`) # Notice, there's no foreign key constraints here anymore # ) 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`) )
Without going into the first two tables, let's focus on the third table, GoodsMessage_CoodsPicture.
Django uses this table to record a piece of data about a certain item corresponding to a certain image. There is a UNIQUE constraint stating that there can be no duplicate records.
In this way, every time you query the GoodsMessage_CoodsPicture table, you can get the picture corresponding to a particular item.
Their implementation in the database is covered here, so how does Django query this data, there is a good blog post recommended:
A comprehensive explanation of Django ORM, one-to-one, one-to-many, and many-to-many based on the
This description of Django foreign key relationship above is all I have to share with you, I hope it can give you a reference, and I hope you will support me more.