假设在项目中有app01和app02两个应用,两个应用的models.py文件中分别有User和Book两个类,要将app02中Book类的author字段设置外键,与app01中的User类关联起来,方法如下:
1、首先在app02的models.py文件中引入User,如下:
from app01.models import User
2、设置外键字段为:
author = models.ForeignKey(to=User,on_delete=models.CASCADE)
这其中需要注意的是,User
一定不能加引号,否则会报以下错误:
video.Video.videoAuthor: (fields.E300) Field defines a relation with model ‘User’, which is either not installed, or is abstract.