I'm getting a circular import error in django and can't seem to solve it. here's my models.py in affiliate(app)
from member.models import Member
class SubAffiliate(models.Model):
member_id = models.ForeignKey(Member, on_delete=models.CASCADE)
and here's my models.py in member(app)
from affiliate.models import Affiliate
class Member(models.Model):
affiliates = models.ManyToManyField(Affiliate, blank=True, related_name="members_affiliate")
for solving the problem I tried importing like this
import affiliate.models
and there use it like this
affiliate.models.Affiliate
then I get this error AttributeError: module 'affiliate' has no attribute 'models'
what should I do to resolve this error. Thank You!
CodePudding user response:
The two models can not import each other. You can work with a string literal in case you need to refer to another module's model:
# no import from member.models!
class SubAffiliate(models.Model):
member = models.ForeignKey(
'member.Member',
on_delete=models.CASCADE
)
and for the other models.py
file, you can also work with a string with the app_name.ModelName
:
# no import from affiliate.models
class Member(models.Model):
affiliates = models.ManyToManyField(
'affiliate.Affiliate',
blank=True,
related_name='members_affiliate'
)