Django foreign key and table relationship

Foreign key delete operation

If a model uses foreign keys. What kind of operation should be performed after the other party’s model is deleted? Can be specified by on_delete. The types that can be specified are as follows.

  1. CASCADE: Cascade operation. If the piece of data corresponding to the foreign key is deleted, then this piece of data will also be deleted.
  2. PROTECT: Protected. That is, as long as the data refers to the data of the foreign key, the data of the foreign key cannot be deleted.
  3. SET_NULL: Set to null. If the data of the foreign key is deleted, then set this field to be empty on this data. If you set this option, the premise is to specify that this field can be empty.
  4. SET_DEFAULT: Set the default value. If the data of the foreign key is deleted, then this field will be set as the default value on this data. If you set this option, the premise is to specify a default value for this field.
  5. SET(): If the data of the foreign key is deleted. Then the value in the SET function will be obtained as the value of this foreign key. The SET function can receive an object that can be called (such as a function or method). If it is an object that can be called, it will return the result of calling this object as a value.
  6. DO_NOTHING: Do not take any action. It all depends on database-level constraints.

Note: The above options are only at the Django level, and the data level is still RESTRICT!

Table relationship

The relationships between tables are all related through foreign keys. The relationship between tables is nothing more than three relationships: one-to-one, one-to-many, many-to-many, and so on. The following will discuss the application scenarios and implementation methods of the three relationships.

One to many

Application scenarios: such as the relationship between the article and the author. An article can only be written by one author, but one author can write multiple articles. The relationship between the article and the author is a typical many-to-one relationship.

Implementation method: One-to-many is achieved through ForeignKey.

class User(models.Model):
    username = models.CharField(max_length=20)
    password = models.CharField(max_length=100)

class Article(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    author = models.ForeignKey("User",on_delete=models.CASCADE)

Then in the future to specify the category to the Article object, you can use the following code to complete.

article = Article(title='abc',content='123')
author = User(username='jkc',password='123456')
# Must be saved to the database first
author.save()
article.author = author
article.save()

And if you want to get all the articles in a certain category in the future, you can do it through article_set. The sample code is as follows.

user = User.objects.first()
# Get all articles written by the first user
articles = user.article_set.all()
for article in articles:
    print(article)

One to one

One-to-one in Django is achieved through models.OnetToOneField. This OneToOneField is actually a foreign key in essence, but this foreign key has a unique key to achieve one-to-one.

If you want to back-reference in the future, you can access it by converting the name of the referenced model to lowercase. For example, the following model.

 class FrontUser(models.Model):
     username = models.CharField(max_length=200)

 class UserExtension(models.Model):
     school = models.CharField(max_length=100)
     user = models.OneToOneField("FrontUser",on_delete=models.CASCADE)

 # Access UserExtension objects through userextension
 user = FrontUser.objects.first()
 print(user.userextension)

The object of UserExtension can access the corresponding user object through user. And the FrontUser object can use userextension to access the corresponding UserExtension object. If you don’t want to use Django’s default reference attribute name. Then you can add a related_name parameter to OneToOneField. The sample code is as follows.

class FrontUser(models.Model):
     username = models.CharField(max_length=200)

 class UserExtension(models.Model):
     school = models.CharField(max_length=100)
     user = models.OneToOneField("FrontUser",on_delete=models.CASCADE,related_name='extension')

 # Access to UserExtension object through extension
 user = FrontUser.objects.first()
 print(user.extension)

Then the FrontUser object can access the corresponding UserExtension object through the extension attribute.

Many to many

Application scenarios: such as the relationship between articles and tags. An article can have multiple tags, and a tag can be cited by multiple articles. Therefore, the relationship between tags and articles is a typical many-to-many relationship.

Implementation: Django provides a dedicated Field for this many-to-many implementation. Called ManyToManyField. Let’s take articles and tags as examples to explain. The sample code is as follows.

class Article(models.Model):
     title = models.CharField(max_length=100)
     content = models.TextField()
     tags = models.ManyToManyField("Tag",related_name="articles")

 class Tag(models.Model):
     name = models.CharField(max_length=50)

At the database level, Django actually establishes an intermediate table for this many-to-many relationship. This intermediate table defines two foreign keys respectively, which refer to the primary keys of the article and tag tables.

When we use many-to-many backreferences to add, we can only use the add method, such as adding tags to the article, the sample code is as follows.

article = Article.objects.first()
tag = Tag(name="good")
tag.save()
article.tag_set.add(tag)  # Add tag to the article

Leave a Reply