Import: cannot import name 'Usuario' from 'usuarios.models

Asked

Viewed 705 times

0

I have these models where the condo can have multiple apartments and an apartment can have multiple owners, each condo has a syndicate, a sub-index and a manager. I am adding sindico, subsindico and manager in condominium class, however, while performing makemigrations, I have the following error:

from usuarios.models import Usuario
ImportError: cannot import name 'Usuario' from 'usuarios.models

Condominium and Apartment Classes

from django.db import models
from endereco.models import Endereco
from usuarios.models import Usuario

class Condominio(models.Model):
   nome = models.CharField(max_length=255, null=False, blank=False)
   endereco = models.ForeignKey(Endereco, on_delete=models.CASCADE)
   sindico = models.ForeignKey(Usuario, on_delete=models.CASCADE)
   subsindico = models.ForeignKey(Usuario, on_delete=models.CASCADE)
   gerente = models.ForeignKey(Usuario, on_delete=models.CASCADE)


   class Meta:
       db_table = 'condominio'
       verbose_name = 'Condomínio'
       verbose_name_plural = 'Condomínios'

   def __str__(self):
       return self.nome


class Apartamento(models.Model):
   numero = models.CharField(max_length=8)
   condominio = models.ForeignKey('Condominio', on_delete=models.CASCADE)

   class Meta:
       db_table = 'apartamento'
       verbose_name = 'Apartamento'
       verbose_name_plural = 'Apartamentos'

   def __str__(self):
       return self.numero

Classes User and Profile

  class Usuario(AbstractBaseUser, PermissionsMixin):
     nome = models.CharField(max_length=255, null=False, blank=False)
     perfil_usuario = models.OneToOneField('PerfilUsuario', on_delete=models.CASCADE) 
     sexo = models.CharField(max_length=1, choices=SEXO)
     data_nascimento = models.DateField(null=False, blank=False)
     cpf = models.CharField(max_length=14, null=False, blank=False, unique=True)
    email = models.EmailField(null=False, blank=False, unique=True)
    telefone1 = models.CharField(max_length=12, null=False, blank=False)
    telefone2 = models.CharField(max_length=12, null=False, blank=False)
    apartamento = models.ForeignKey(Apartamento, on_delete=models.CASCADE)
    proprietario = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['nome']

    class Meta:
       db_table = 'usuario'
       verbose_name = 'Usuário'
       verbose_name_plural = 'Usuários'

   @property
   def get_primeiro_nome(self):
       return self.nome.split(' ')[0]

   def salvar(self, *args, **kwargs):
       super(Usuario, self).save()

   def __str__(self):
       return self.nome


class PerfilUsuario(models.Model):
    tipo_usuario = models.CharField(max_length=2, choices=TIPO_USUARIO, verbose_name='Tipo de Usuário')
    desabilitado = models.BooleanField(default=False)

    class Meta:
        db_table = 'perfil_usuario'
        verbose_name = 'Perfil do Usuário'

   @property
   def get_usuario(self):
       return self._usuario.nome

   def __str__(self):
       return self.tipo_usuario

TIPO_USUARIO = [
    ('S', 'Síndico'),
    ('SS', 'Subsíndico'),
    ('G', 'Gerente'),
    ('M', 'Morador'),
]

Stacktrace

Link to the full Stacktrace

[EDIT]

In other directories import normally occurs, but in this specific one, no

Example where import is normally occurring

from django.db import models
from usuarios.models import Usuario
from choices.tipo_aviso import TIPO_AVISO


class Aviso(models.Model):
    titulo = models.CharField(max_length=255)
    descricao = models.CharField(max_length=500)
    usuario = models.ForeignKey(Usuario, on_delete=models.CASCADE)
    tipo_aviso = models.CharField(max_length=1, choices=TIPO_AVISO, verbose_name='Tipos de Aviso')

    class Meta:
        db_table = 'aviso'
        verbose_name = 'Aviso'
        verbose_name_plural = 'Avisos'

    def __str__(self):
         return self.descricao
  • you’re sure it exists Usuario in usuarios.models?

  • Yes, there is, even in other models of other apps of the project I can import the user normally, however, in this specific, no

  • From what deretorio are you doing import? It seems to me that the problem is in the "way" of import, for example, if you are in the same user directory, you should do from .models import usuario

  • The classes Condominium and Apartment, are in a directory and User and Profile are in another directory

  • strange, we know it’s a path error, probably some writing error, check if the names match.

1 answer

1


Delete the section from usuarios.models import Usuario and do the following:

usuario = models.ForeignKey('usuarios.Usuario', on_delete=models.CASCADE)

The same is valid for all snippets where you want to add the relationship, delete the import and refer to the class as mentioned above.

  • That settles it for me, Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.