Cross imports in Django

3

In my Django project I have 2 apps, as the following map shows:

Django/ProyectoWeb/Apps/
├── Catedra
└── Usuarios

Without detailing much:

  • The Chair has a unique model called Commission.

  • Users have 2 models called Student, Teacher.

The problem is that the commission has a teacher in charge and the student has an assigned commission. For this I have to import in the following way:

  • In apps.Catedras.models importo: from Apps.Usuarios.models import Docente

  • In apps.Usuarios.models importo: from Apps.Catedras.models import comision

But when doing these imports, and running the makemigrations , it tells me that it is not possible to import.

I do not know if it is understood. Anything I try to clarify it better

Edit

  

Apps.usuarios.models

from __future__                         import unicode_literals

from django.db                          import models
from django.contrib.auth.models         import User

from Apps.catedras.models               import ComisionCatedra
from Apps.choices                       import (
                                            ESPECIALIDADES_CHOICES,
                                            MATERIAS_CHOICES,
                                            )
class Alumno(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, default=1)
    fecha_nacimiento = models.DateField(null=True)
    dni = models.PositiveIntegerField()
    legajo = models.CharField(max_length=10)
    comision = models.ForeignKey(ComisionCatedra)
    especialidad = models.CharField(max_length=50, choices=ESPECIALIDADES_CHOICES)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True, null=True)


class Docente(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, default=1)
    dni = models.PositiveIntegerField() 


class Laboratorista(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, default=1)
    dni = models.PositiveIntegerField()
  

Apps.catedras.models

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

from Apps.choices                       import (
                                            DIAS_SEMANA_CHOICES,
                                            ESPECIALIDADES_CHOICES,
                                            MATERIAS_CHOICES,
                                            )
from Apps.usuarios.models               import (
                                            Alumno,
                                            Docente,
                                            Laboratorista,
                                            )


class ComisionCatedra(models.Model):
    numero = models.PositiveIntegerField()
    especialidad = models.CharField(max_length=50, choices=ESPECIALIDADES_CHOICES)
    materia = models.CharField(max_length=50, choices=MATERIAS_CHOICES)
    titular = models.ManyToManyField(Docente)
    adjunto = models.ManyToManyField(Docente)
    jtp = models.ManyToManyField(Docente)
    ayudantes = models.ManyToManyField(Docente)

The message in the console when executing makemigrations is:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 337, in execute
    django.setup()
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
  File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/jorger/Django/fisicaweb/Apps/laboratorio/models.py", line 16, in <module>
    from Apps.usuarios.models               import (
  File "/home/jorger/Django/fisicaweb/Apps/usuarios/models.py", line 7, in <module>
    from Apps.catedras.models               import ComisionCatedra
  File "/home/jorger/Django/fisicaweb/Apps/catedras/models.py", line 11, in <module>
    from Apps.usuarios.models               import (
ImportError: cannot import name Alumno
    
asked by Jorge Ernesto RONCONI 20.07.2017 в 03:02
source

1 answer

2

This usually happens when you have circular import problems (module A imports something from B, module B imports something from A).

The easiest way is to use the names of the models using the app.Modelo notation:

class ComisionCatedra(models.Model):
    # ...
    titular = models.ManyToManyField('usuarios.Docente')
    adjunto = models.ManyToManyField('usuarios.Docente')
    jtp = models.ManyToManyField('usuarios.Docente')
    ayudantes = models.ManyToManyField('usuarios.Docente')

You do not need to do import , this is handled by Django internally. The same would do in your other models.

    
answered by 20.07.2017 / 20:34
source