Django -- library management system (6)

zy010101 2021-01-21 07:58:44
django library management

This article will talk about setting foreign key operation , More about foreign keys , You can look at the knowledge of the database . Of course, I will still make some explanations in the notes .

Now we have the list of publishers , Now let's add the book table . Because there is a relationship between publishing houses and books , We use foreign keys to connect the two . stay Is as follows :

from django.db import models
# Create your models here.
There is a relationship between books and publishers . The book is published by a publishing house .
For example, the Mechanical Industry Press published 《 Data structure and algorithm analysis ——C Language description 》 This book .
that , The list of publishers below us needs to have a relationship with the list of books , A publishing house can publish more than one book .
class Publisher(models.Model):
Django Of models The primary key will be created in the database by default
name = models.CharField(max_length=128) # Publisher name
class Book(models.Model):
name = models.CharField(max_length=128) # Title
publisher_id = models.ForeignKey('Publisher', on_delete=models.CASCADE)
# By default, foreign keys are associated with Publisher Primary Key .
ForeignKey The first parameter of is the class name of the table to be associated with , You can write the class name directly , But this time it must be the class defined in the previous article .
You can also change the class name into a string , In this way, the class is not required to be defined in the previous article .
The second parameter on_delete stay Django2.0 It must be filled in after that ,models.CASCADE Indicates cascade delete . Cascade deletion means , I deleted a publishing house , Then all the books corresponding to the publishing house have also been deleted .
models.PROTECT: Protect , If a publishing house still has books , Then it is not allowed to delete the publishing house .
models.SET_DEFAULT: It means to set it as the default value after deletion
models.SET_NULL: Set to null after deletion
models.SET(x): Set to a certain value after deletion

After adding a table , We should execute the database migration command . As shown in the figure below .

This completes the migration of the database . after , You will find that the structure of the table is as follows .

publisher_id_id It's a strange name , This is because models Will automatically add _id This suffix , therefore , Modify our Book The categories are as follows :

class Book(models.Model):
name = models.CharField(max_length=128) # Title
publisher = models.ForeignKey('Publisher', on_delete=models.CASCADE)

Revised models after , The database migration command must be re executed .

After database migration , As shown below :

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .


  1. Python 爬虫进阶 - 前后端分离有什么了不起,过程超详细!
  2. 【python】使用pip提示ModuleNotFoundError
  3. 【python】虚拟环境搭建
  4. Advanced test | Python written test questions
  5. Fire! Open source Python ticket grabbing artifact, come home to see this wave of New Year!
  6. Python crawler advanced - before and after the end of the separation of what great, super detailed process!
  7. [Python] prompt modulenotfounderror with PIP
  8. Building a virtual environment
  9. Serverless 架构下用 Python 轻松搞定图像分类和预测
  10. Easy image classification and prediction with Python under serverless architecture
  11. python协程爬取某网站的老赖数据
  12. Python coroutine crawls Laolai data of a website
  13. 使用Python分析姿态估计数据集COCO的教程
  14. Using Python to analyze the data set coco of attitude estimation
  15. win环境 python3 flask 上手整理 环境搭建(一)
  16. Getting started with win environment python3 flash
  17. Python实现一个论文下载器,赶紧收藏
  18. win环境 python3 flask 上手整理 快速上手-基础操作(二)
  19. Python 中常见的配置文件写法
  20. Python to achieve a paper Downloader, quickly collect
  21. Python批量 png转ico
  22. 使用line_profiler对python代码性能进行评估优化
  23. 使用line_profiler对python代码性能进行评估优化
  24. Getting started with Python 3 flash in win environment
  25. Common ways to write configuration files in Python
  26. Python会在2021年死去吗? Python 3.9最终版本的回顾
  27. Python batch PNG to ICO
  28. Using line_ Profiler evaluates and optimizes the performance of Python code
  29. Using line_ Profiler evaluates and optimizes the performance of Python code
  30. Will Python die in 2021? A review of the final version of Python 3.9
  31. Python3 SMTP send mail
  32. Understanding closures in Python: getting started with closures
  33. Python日志实践
  34. Python logging practice
  35. [python opencv 计算机视觉零基础到实战] 十、图片效果毛玻璃
  36. [python opencv 计算机视觉零基础到实战] 九、模糊
  37. 10. Picture effect ground glass
  38. [Python opencv computer vision zero basis to actual combat] 9. Fuzzy
  39. 使用line_profiler對python程式碼效能進行評估優化
  40. Using line_ Profiler to evaluate and optimize the performance of Python code
  41. LeetCode | 0508. 出现次数最多的子树元素和【Python】
  42. Leetcode | 0508
  43. LeetCode | 0530. 二叉搜索树的最小绝对差【Python】
  44. LeetCode | 0515. 在每个树行中找最大值【Python】
  45. Leetcode | 0530. Minimum absolute difference of binary search tree [Python]
  46. Leetcode | 0515. Find the maximum value in each tree row [Python]
  47. 我来记笔记啦-搭建python虚拟环境
  48. Let me take notes - building a python virtual environment
  49. LeetCode | 0513. 找树左下角的值【Python】
  50. Leetcode | 0513. Find the value in the lower left corner of the tree [Python]
  51. Python OpenCV 泛洪填充,取经之旅第 21 天
  52. Python opencv flood fill, day 21
  53. Python爬虫自学系列(二)
  54. Python crawler self study series (2)
  55. 【python】身份证号码有效性检验
  56. [Python] validity test of ID number
  57. Python ORM - pymysql&sqlalchemy
  58. Python ORM - pymysql&sqlalchemy
  59. centos7 安装python3.8
  60. centos7 安装python3.8