赞
踩
# 这样写会出现报错: Django ImportError:cannot import name '...' from partially initialized module'...' (most likely due to a circular import)
# app1的models.py
from app2.models import B
class L1(models.Model):
b = models.ForeignKey(B, on_delete=models.CASCADE)
# app2的models.py
from app1.models import A
class L2(models.Model):
a = models.ForeignKey(A, on_delete=models.CASCADE)
# app_name必须是在settings.py中注册的app的名称,直接在后面.模型名就可以引用了
# app1的models.py
# 不能在最外层引用from app2.models import B
class L1(models.Model):
b = models.ForeignKey("app_name.B", on_delete=models.CASCADE)
# app2的models.py
# 不能在最外层引用from app1.models import A
class L2(models.Model):
a = models.ForeignKey("app_name.A", on_delete=models.CASCADE)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。