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

252
Vistas
Importing from views to models in Django

I have something like this in one of my apps's models.py :

class Account(AbstractBaseUser):
    email = models.EmailField(unique=True)

I want to import a class from another app's views.py as below:

from anotherapp.views import MyClass

The problem is in the first lines of anotherapp.views file, I imported the Account class. So when I want to import MyClass into my models.py file, this error raises :

ImportError: cannot import name Account
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

That is circular import error you are encountering. While it is bad practice to import from views to models in Django, if you still want to, you can follow methods in this question to resolve it.

Here are few alternative ways that can be considered as good practice:

  • You can consider importing other low-level modules exist within anotherapp instead of MyClass (not depending on views.py)
  • You can use Django's signals to catch anotherapp's events project wide and act according to.
  • You can create a third file, say utils.py in anotherapp, move MyClass there and let anotherapp/views.py and your app's models.py import from anotherapp.utils
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