Problem: No name 'departments' in module 'funcionario.apps'

Asked

Viewed 22 times

0

I cannot pass this phase because when compiling it presents the error : No name 'departments' in module 'funcio.apps'

from django.db import models
from django.contrib.auth.models import User
from .apps.departamento.models import Departamento


class Funcionario(models.Model):
    nome =  models.CharField(max_length=100, help_text='Nome do Funcionario')
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    departamentos = models.ManyToManyField(Departamento)

    def __str__(self):
        return self.nome

1 answer

0


Well, the error is self-descriptive.

What he says is that there is the name "department"- the prefix .apps tells Python - the language - that in the directory where this file is - that possibly is in another "app" of Django, there is a folder apps, and inside that folder he could find a directory with name departamentos or a file called departamentos.py. Not the case - if your Django project is structured in the way described in the documentation for Django, you have an "app" "departments" sister app "employee" a folder above.

Import will work if you import the "departments" package directly, once Django configures Pythonpath to find the apps installed in the project:

from departamento.models import Departamento

Unless mistaken, Django’s ORM is also smart enough to find the template from a string, with the name_da_app.template name_do_template - in this case, you don’t even need to import -just change your field statement to:

departamentos = models.ManyToManyField("departamentos.Departamento")

(Django’s ORM has this feature - the "saved mistake" is that I don’t remember if anything else has to be put in the string to address the other model correctly).

Browser other questions tagged

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