[GO] Associate the table set in python models.py

What you want to do

I'm making a blog using django in python. When setting the table in models.py I want to create a table that has a parent-child relationship like Category> Article.

manner

To link each table Use the variables that exist in each table. Set the variable specified at that time as foreign_key.

I actually tried it

The article posted on the blog contains the following classes and data. In the post class id,title,cantent,created_at,category At that time, create a separate category class and id,name,created_at It is composed of.

If you decide the data type and drop them in the code,

class Category(models.Model):
    name = models.CharField('Category name',max_length=255)
    created_at = models.DateTimeField('date',default=timezone.now)

class Day(models.Model):
    title = models.CharField('title',max_length=200)
    text = models.TextField('Text')
    date = models.DateTimeField('date',default=timezone.now)
    category = models.ForeignKey(Category,verbose_name='category',on_delete=models.PROTECT)

Assigned to the category instance of the Day class is the product of models multiplied by the ForeignKey function. At that time, you can take category as the first argument and change the name of the model to any one with verbose_name. And by assigning the PROTECT function to on_delete, even when the deletion process is performed by the category class, the deletion will not be performed as long as the post class exists (the posted article will not be deleted even if the category is deleted).

Summary

By using the ForeignKey function The setting source is the parent table, and the referenced side is the small table and is associated.

Reference article ForeignKey:https://www.sejuku.net/blog/54072 verbose_name:https://codor.co.jp/django/how-to-use-verbose-name

Recommended Posts

Associate the table set in python models.py
[Automation] Extract the table in PDF with Python
Download the file in Python
Set python test in jenkins
Manipulate the clipboard in Python and paste the table into Excel
Getting the arXiv API in Python
Python in the browser: Brython's recommendation
Save the binary file in Python
Get the desktop path in Python
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Hit the web API in Python
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
Get the host name in Python
Access the Twitter API in Python
The first step in Python Matplotlib
I wrote the stack in Python
Master the weakref module in Python
[Python] Let's reduce the number of elements in the result in set operations
Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
Load the remote Python SDK in IntelliJ
Try using the Wunderlist API in Python
[Python] Set the graph range with matplotlib
Check the behavior of destructor in Python
Try using the Kraken API in Python
Learn the design pattern "Observer" in Python
Learn the design pattern "Proxy" in Python
Write the test in a python docstring
Learn the design pattern "Command" in Python
OR the List in Python (zip function)
Display Python 3 in the browser with MAMP
To set default encoding to utf-8 in python
Tweet using the Twitter API in Python
Learn the design pattern "Visitor" in Python
Learn the design pattern "Bridge" in Python
Check if the URL exists in Python
Learn the design pattern "Mediator" in Python
Learn the design pattern "Decorator" in Python
Run the Python interpreter in a script
The result of installing python in Anaconda
How to display multiplication table in python
What is "mahjong" in the Python library? ??
Read the file line by line in Python
Read the file line by line in Python
MongoDB for the first time in Python
Learn the design pattern "Iterator" in Python
The basics of running NoxPlayer in Python
Learn the design pattern "Strategy" in Python
Learn the design pattern "Composite" in Python
Solve the maximum subarray problem in Python
In search of the fastest FizzBuzz in Python
Set the process name of the Python program
Learn the design pattern "State" in Python
Try hitting the YouTube API in Python
Get the EDINET code list in Python
Learn the design pattern "Adapter" in Python