Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
Running into a Circular Import issue

I have two apps(front and lrnadmin) in project(portal) this is my root directory

enter image description here

the problem is that when I import the models in each other it gives me error In this models I want Qualification as Foreign key that why I want import Qualification from lrnadmin.models this is my front.models.py

from datetime import datetime, timezone
from django.db import models
from django.db.models import expressions
from django.db.models.base import Model
from django.utils import timezone

from lrnadmin.models import Qualification

class Staff(models.Model):
    """Model definition for Staff."""
    # TODO: Define fields here
    nStaff = models.CharField(max_length= 50)
    role = models.CharField(max_length= 50)
    lrnQ = models.ForeignKey(Qualification, on_delete= models.CASCADE)
    relQ = models.CharField(max_length=200)
    relE = models.CharField(max_length=200)
    # Cv = models.CharField(max_length=100, blank=True, null=True)
    # Doc = models.CharField(max_length=100, blank=True, null=True)
    sec7 = models.ForeignKey(Sec7, on_delete=models.CASCADE)
    class Meta:
        """Meta definition for Staff."""

        verbose_name = 'Staff'
        verbose_name_plural = 'Staffs'

    # def __str__(self):
    #     """Unicode representation of Staff."""
    #     pass

and in this models I want User as Foreign key that why I want import User from front.models this is my lrnadmin.models.py

from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils import timezone
from front.models import User

class CentreCode(models.Model):
    """Model definition for CentreCode."""

    # TODO: Define fields here
    user = models.ForeignKey(User, on_delete= models.CASCADE)
    centreCode = models.CharField(max_length= 20)
    categories = models.CharField(max_length = 20)
    authorisation = models.CharField(max_length= 20)
    registrationType = models.CharField(max_length= 20)

    class Meta:
        """Meta definition for CentreCode."""

        verbose_name = 'CentreCode'
        verbose_name_plural = 'CentreCodes'

    # def __str__(self):
    #     """Unicode representation of CentreCode."""
    #     pass

How Can I handle this

File "E:\lrn\portal\lrnadmin\models.py", line 4, in <module>
    from front.models import User
  File "E:\lrn\portal\front\models.py", line 7, in <module>
    from lrnadmin.models import Qualification
ImportError: cannot import name 'Qualification' from partially initialized module 'lrnadmin.models' (most likely due to a circular import) (E:\lrn\portal\lrnadmin\models.py)
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can "lazy-load" a model by using a string literal as described in the docs, so for example with the front app's User model you can define it like:

# Remove from front.models import User


class CentreCode(models.Model):
    # ...
    user = models.ForeignKey('front.User', on_delete= models.CASCADE)
    # ...

Or if you defined front.models.User as a custom user model, it's highly suggested that you just reference the custom model using settings.AUTH_USER_MODEL as described here:

from django.conf import settings
# Remove from front.models import User


class CentreCode(models.Model):
    # ...
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete= models.CASCADE)
    # ...
over 4 years ago · Santiago Trujillo Relatório

0

you can try out the get_model which will avoid importing the model at run time

   from django.apps import apps
   qualification_model = app.get_model('lrnadmin', 'Qualification')

OR

you can try to import the model inside the function instead of importing at module level

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda