多个app的模型间引用外键
创建一个新的app, 并设计一个Reader模型
from django.db import models
class Reader(models.Model):
name = models.CharField(max_length=20)
回到Article模型中, 设置reader外键:
class Article(models.Model):
title = models.CharField(max_length=20)
content = models.TextField()
author = models.ForeignKey('Person', on_delete=models.CASCADE)
# 设置外键 允许为空
reader = models.ForeignKey('reader.Reader', on_delete=models.CASCADE, null=True)