Home > OS >  what does .models mean in Django?
what does .models mean in Django?

Time:11-28

I am trying to import a class named 'Questions' from my models.py to admin.py from .models import Questions I don't understand why we have to use a period in '.models', what does it mean and what exactly is it pin pointing to?

I tried this combinations but it was no avail from models import Questions from Model.models import Questions

CodePudding user response:

This is due to relative import. you see their are 2 types of import statement:

  1. Absolute import: they start from the root of the project. Like in Django where the manage.py is, that is the root. So your import statement can be written as

    from {AppName}.models import Questions

  2. But here you are using relative path. Relative because you start from the current directory you are in and not the root. So in .models the . actually means current directory. You can not use models because there is no models in root directory.

For more in depth detail try this Python imports

CodePudding user response:

That's a relative import. It means go back one level in the folder tree and import models.

This:

from .models import Questions

is the same as this:

from my_folder_containing_models.models import Questions
  • Related