Django中app的model相互引用問題


Django有倆個 app 

----------在 Course 的models.py中:

from Shopping.models import Coupon, OrderDetail

class Course:
  order_details = GenericRelation(to=OrderDetail)
  coupon = GenericRelation(to=Coupon)

-------------在Shopping的models.py中:

from Course.models import Account

class Coupon:
  account = models.ForeignKey(to=Account, verbose_name="擁有者", on_delete=None, related_name="coupons")

class OrderDetail:
  pass

報錯:ImportError: cannot import name 'xxx'

原因:暫不清楚

解決方案:使用app_name.class_name的方式,注意不是app_name.models.class_name

在 Course 的models.py中:

class Course:
  order_details = GenericRelation(to="Shopping.OrderDetail")
  coupon = GenericRelation(to="Shopping.Coupon")

在Shopping的models.py中:

class Coupon:
  account = models.ForeignKey(to="Course.Account", verbose_name="擁有者", on_delete=None, related_name="coupons")

class OrderDetail:
  pass

 

 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM