Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda