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

310
Views
ImportError: cannot import name 'Maca' from partially initialized module 'maca.models' (most likely due to a circular import)

I have this error ImportError: cannot import name 'Maca' from partially initialized module 'maca.models' (most likely due to a circular import).

I have code like this

from maca.models import Maca
class Maca2(models.Model)
   maca = models.ForeignKey(
     Maca, on_delete=models.CASCADE
)

Now to model "Maca" I'm trying to access every single "Maca2" objects like this

from maca2.models import Maca2
class Maca(models.Model)
  ...


@property
maca_has_maca2(self)
  maca2 = Maca2.objects.filter(maca=self.id)

Can you help me to handle this?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can import the Maca2 in the maca_has_maca2 property:

# no import of maca2.models

class Maca(models.Model):
    # …

    @property
    def maca_has_maca2(self):
        from maca2.models import Maca2
         maca2 = Maca2.objects.all()

For ForeignKeys, OneToOneFields and ManyToManyFields, you can make use of a string literal with as structure 'app_name.ModelName' to refer to a model, for example:

# no import of maca2.models

class Maca(models.Model):
    maca2 = models.ForeignKey(
        'maca2.Maca2', on_delete=models.CASCADE
    )

This avoids importing modules and thus circular imports. If the model has the same app_name, you can reference this by 'ModelName'.

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!