Django foreign key constraints

ORM foreign key constraints

from django.db import models
 
 
# 出版社
class Publisher(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=64, null=False, unique=True)
 
    def __str__(self):
        return "<Publisher object: {}>".format(self.name)
 
 
# 书籍
class Book(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=64, null=False, unique=True)
    publisher = models.ForeignKey(to="Publisher")
    
    def __str__(self):
        return "<Book object: {}>".format(self.title)

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-pq3CG3lE-1648097715132)(%E5%A4%96%E9%94%AE%E7%BA%A6% E6%9D%9F.assets/1648097465056.png)]

Guess you like

Origin blog.csdn.net/m0_49501453/article/details/123708280