Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
ImportError: cannot import name 'Affiliate' from partially initialized module 'affiliate.models' (most likely due to a circular import)

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!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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'
    )
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!