4
I’m working on a multi-app project. The department app model is as follows::
from emails.models import GerenciarEmails
class Departamento(models.Model):
#modelo de grupos de disciplinas
class Meta:
verbose_name = 'Departamento'
verbose_name_plural = 'Departamentos'
titulo = models.CharField(max_length=255, verbose_name='Nome do Grupo')
watermark = models.FileField(upload_to = settings.MEDIA_ROOT+"/watermarks/", default=settings.MEDIA_ROOT+'/img/generico.gif')
cor = ColorField(blank=True)
gerencia = models.ForeignKey(GerenciarEmails)
docentes_responsaveis = models.ManyToManyField(settings.AUTH_USER_MODEL, null=True, blank=True,related_name="docentes_responsaveis_pelo_grupo", limit_choices_to={'docente':True})
def __unicode__(self):
#Modo como o Modelo e Exibido na Lista na Adminstracao
return unicode(self.titulo)
I added the following instance to it:
gerencia = models.ForeignKey(GerenciarEmails)
Mail management is a method of the email app model. The import what matters is this used, the others are related to other things.
With the insertion of this instance, Django gives the following error: Import: cannot import name Department
Apparently he is not letting enter this instance. Could someone help me on the reason for the error?
This is the method Managerial:
class GerenciarEmails(models.Model):
class Meta:
verbose_name = 'Gerenciar E-mails'
verbose_name_plural = 'Gerenciar E-mails'
departamento = models.ForeignKey(Departamento)
def __unicode__(self):
return self.departamento.titulo
As you can see, I use the Department method, from models.py from the Departments app. If I put import after this method, as is the suggestion from mgibsonbr, python gives another error:
ImportError: cannot import name Disciplina
It kind of makes sense, because Managerial needs the import of Department.
Put the complete traceback error, it will help to solve your problem.
– ppalacios
As in the question of another Stack user, http://stackoverflow.com/questions/17845366/importarror-cannot-import-name I believe it will help you.
– Guilherme Lima
If your app A depends on (imports) B, and B in turn depends on A, there is some problem in your modeling. Probably both models (
Departamento
andGerenciarEmails
) should be part of the same app, and you should create the templates in the right order (dependent before dependent).– mgibsonbr
I think I’ve noticed your problem. Do you have the Department table with an FK for Managementmails, and Managementmails with an FK for Department?! This can’t happen, neither in Django nor in any relational database. Only one can have FK for the other. If the ratio is 1 to 1, choose one of the templates (I suggest Manage Mails) and put a Onetoonefield on it, and only. The other model will automatically have a field for the inverse ratio. But in the DB the foreign key must be in a single table.
– mgibsonbr