当前位置:网站首页>ORM model -- associated fields, abstract model classes
ORM model -- associated fields, abstract model classes
2022-07-07 10:07:00 【chuntian_ tester】
1. Correlation field
The relationship between tables is divided into :
One to one relationship : Such as the relationship between student table and student details table , A student in the student information form
There is only one corresponding relationship record in
One-to-many relation : Such as projects Table and interfaces The relationship between tables , An item in the item list , In the interface table
There may be multiple interfaces in this project
Many to many relationship : Such as the relationship between student schedule and curriculum , A student in the student list may take more than one course in the curriculum
Course , A course in the curriculum , It may also be selected by multiple students in the student list
1.1 establish interfaces Subapplication , And define Interfaces Model class
1.1.1 Project root execute command :python manage.py startapp interfaces
1.1.2 take interfaces Subapplication is registered to setting.py Medium INSTALLED_APPS in
1.1.2 interfaces/models.py Define model classes in Interfaces:
from django.db import models
class Interfaces(models.Model):
"""
1. If you create a one to many relationship , So you need to ' many ' Defined in the model class of models.ForeignKey() Foreign key field
1.1 models.ForeignKey() The first parameter is a required parameter , Specify the parent table model class that needs to be associated : Subapplication name . Model class name ,models.ForeignKey("projects.Projects")
1.2 You can also directly import the parent model class into this file , Use the parent table model class name directly :models.ForeignKey(Projects)
1.3 In foreign keys on_delete Parameters :
1.3.1 models.CASCADE: When the main table record is deleted , Delete from the table record
1.3.2 models.SET_NULL: When the main table record is deleted , Automatically set from table records to null
1.3.3 models.SET_DEFAULT: When the main table record is deleted , Automatically set from table records to default , Additional designation is required default=True
1.3.4 models.PROTECT: When the main table record is deleted , If there is corresponding slave table data , Will throw an exception
2. If you create a one-to-one relationship , It can be defined in any model class models.OneToOneField()
3. If you create many to many relationships , It can be defined in any model class mmodels.ManyToManyField()
"""
id = models.AutoField(primary_key=True, verbose_name='id Primary key ', help_text='id Primary key ')
name = models.CharField(verbose_name=' The name of the interface ', help_text=' The name of the interface ', max_length=50)
tester = models.CharField(verbose_name=' Testers ', help_text=' Testers ', max_length=30)
create_time = models.DateTimeField(auto_now_add=True, verbose_name=' Creation time ', help_text=' Creation time ')
update_time = models.DateTimeField(auto_now=True, verbose_name=' Update time ', help_text=' Update time ')
# Set up foreign keys
projects = models.ForeignKey('projects.Projects', on_delete=models.CASCADE,
verbose_name=' Project ', help_text=' Project ')
class Meta:
db_table = ' Interface table '
verbose_name = ' Interface table '
# Plural form
verbose_name_plural = ' Interface table '
# ordering Define the fields to sort
ordering = ['id']
1. If you create a one to many relationship , So you need to ' many ' Defined in the model class of models.ForeignKey() Foreign key field 1.1 models.ForeignKey() The first parameter is a required parameter , Specify the parent table model class that needs to be associated : Subapplication name . Model class name ,models.ForeignKey("projects.Projects") 1.2 You can also directly import the parent model class into this file , Use the parent table model class name directly :models.ForeignKey(Projects) 1.3 In foreign keys on_delete Parameters : 1.3.1 models.CASCADE: When the main table record is deleted , Delete from the table record 1.3.2 models.SET_NULL: When the main table record is deleted , Automatically set from table records to null 1.3.3 models.SET_DEFAULT: When the main table record is deleted , Automatically set from table records to default , Additional designation is required default=True 1.3.4 models.PROTECT: When the main table record is deleted , If there is corresponding slave table data , Will throw an exception 2. If you create a one-to-one relationship , It can be defined in any model class models.OneToOneField() 3. If you create many to many relationships , It can be defined in any model class mmodels.ManyToManyField()
1.2 Perform the migration
2. Abstract model class
above Interfaces The model class defines create_time and update_time, Parent table Projects These two fields are also defined , So we can create utils Catalog , Define some public model classes in it BaseModel, Similar toolbox , In this way, you can directly import and reference public model classes in different sub applications
2.1 establish utils Catalog
2.2 Define common model classes BaseModel
2.2.1 take BaseModel Defined as abstract class , tell ORM frame , This class is only used to be inherited , Do not
Create the corresponding table with :abstract= True
2.3 Reference inheritance in subapplication BaseModel
2.3 Interfaces Subapplication inheritance BaseModel The public model class is simplified as follows :
from django.db import models
from utils.base_model import BaseModel
# class Interfaces(models.Model):
class Interfaces(BaseModel):
"""
1. If you create a one to many relationship , So you need to ' many ' Defined in the model class of models.ForeignKey() Foreign key field
1.1 models.ForeignKey() The first parameter is a required parameter , Specify the parent table model class that needs to be associated : Subapplication name . Model class name ,models.ForeignKey("projects.Projects")
1.2 You can also directly import the parent model class into this file , Use the parent table model class name directly :models.ForeignKey(Projects)
1.3 In foreign keys on_delete Parameters :
1.3.1 models.CASCADE: When the main table record is deleted , Delete from the table record
1.3.2 models.SET_NULL: When the main table record is deleted , Automatically set from table records to null
1.3.3 models.SET_DEFAULT: When the main table record is deleted , Automatically set from table records to default , Additional designation is required default=True
1.3.4 models.PROTECT: When the main table record is deleted , If there is corresponding slave table data , Will throw an exception
2. If you create a one-to-one relationship , It can be defined in any model class models.OneToOneField()
3. If you create many to many relationships , It can be defined in any model class mmodels.ManyToManyField()
"""
name = models.CharField(verbose_name=' The name of the interface ', help_text=' The name of the interface ', max_length=50)
tester = models.CharField(verbose_name=' Testers ', help_text=' Testers ', max_length=30)
# Set up foreign keys
projects = models.ForeignKey('projects.Projects', on_delete=models.CASCADE,
verbose_name=' Project ', help_text=' Project ')
class Meta:
db_table = ' Interface table '
verbose_name = ' Interface table '
# Plural form
verbose_name_plural = ' Interface table '
# ordering Define the fields to sort
ordering = ['id']
2.4 Execute migration script
python manage.py makemigrations projects interfaces
python manage.py migrate interfaces
python manage.py migrate projects
边栏推荐
- 根据热门面试题分析Android事件分发机制(二)---事件冲突分析处理
- Internship log - day07
- 喜马拉雅网页版每次暂停后弹窗推荐下载客户端解决办法
- 官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
- CodeForces - 1324D Pair of Topics(二分或双指针)
- 网上可以开炒股账户吗安全吗
- Qualifying 3
- Introduction to automated testing framework
- [Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
- Deadlock caused by non clustered index in SQL Server
猜你喜欢
Postman interface test I
Win10安装VS2015
字节跳动 Kitex 在森马电商场景的落地实践
web3.0系列之分布式存储IPFS
Garbage disposal method based on the separation of smart city and storage and living digital home mode
Basic use of JMeter to proficiency (I) creation and testing of the first task thread from installation
基础篇:带你从头到尾玩转注解
CentOS installs JDK1.8 and mysql5 and 8 (the same command 58 in the second installation mode is common, opening access rights and changing passwords)
Delete a record in the table in pl/sql by mistake, and the recovery method
Google Colab装载Google Drive(Google Colab中使用Google Drive)
随机推荐
There is a problem using Chinese characters in SQL. Who has encountered it? Such as value & lt; & gt;` None`
Google Colab装载Google Drive(Google Colab中使用Google Drive)
Huffman encoded compressed file
视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
根据热门面试题分析Android事件分发机制(一)
位操作==c语言2
arcgis操作:dwg数据转为shp数据
反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
2020浙江省赛
Software modeling and analysis
ORM模型--关联字段,抽象模型类
Gym - 102219j kitchen plates (violent or topological sequence)
C# 初始化程序时查看初始化到哪里了示例
Natapp intranet penetration
Selenium+bs4 parsing +mysql capturing BiliBili Tarot data
web3.0系列之分布式存储IPFS
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
In addition to the objective reasons for overtime, what else is worth thinking about?
用flinksql的方式 写进 sr的表,发现需要删除的数据没有删除,参照文档https://do
Guys, how can mysql-cdc convert the upsert message to append only