Error importing Model Django 3.1

Asked

Viewed 29 times

-1

Hello, I am trying to import a model to use inside my View and I am having problems, I made a test file to try to import before putting the model in the view. I already registered the app, I already made the "migrate" of it and even so it says that the model was not found. I tried to import it in the following ways:

from .models import *
from .models import ClassNameHere
from my_app.models import ClassNameHere

My model is in the same directory as the file I will use it in. It follows my code:

MY TEST FILE:

from student_base.models import Words

print('test')

My file of project Settings:

INSTALLED_APPS = [ 
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'student_base',
    'django.contrib.sites',
    'user_authentication',
    #autentication
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'pages.apps.PagesConfig',
    'crispy_forms',
    #My windown loggin

If I try to import the model and print a "Hello, world" later just to see if the import is occurring, the following error appears:

ImportError: attempted relative import with no known parent package

I already registered the app in Settings as mentioned

  • adds on the question to your project tree, doing the favor

2 answers

0

Solved:

Guys, I appreciate the help. Carlos Cortez’s answer gave me a glimpse. He said "well, he was imported. It just hasn’t been used yet."Well, I’m new to Django, I’m learning from the documentation. What happened was this: I did all the procedures correctly, and before trying to instantiate an object from my model’s class in my view, I simply created a "test.py" file where I did "from . model import myModelHere" and printed a "Hello word". I thought "If no error appears, it will print the message and my model was imported successfully!". That was the error... But when I applied it to the view, it worked right.

-1

instead of:

From .models import *

uses lower case letter:

from .models import ModelAlpha, ModelBeta, NomeQualquerModel

there is also the possibility that your file is under a different name, so it could be:

from .model import *

If none of this works, delete this . py file that contains the templates and create another one. Sometimes, when you create a file and change its name after it gives some weird problems that are not worth trying to solve. It is easier to delete and make another. (Obviously you will copy the contents of the file before, so you do not need to write everything again.)

Edit1(supplement):

may sound disrespectful, but the question is a little vague, so some things need to be stated in order to reach a solution to your question.

Apparently in your file py views. You’re trying to do the following:

from student_base.models import Words

print('test')

and the message you’re finding strange is that Model Words was not imported.

well, it was imported. It just hasn’t been used yet.

For you to make use of it, you need codar, for example:

from student_base.models import Words


def funcao_qualquer(request):
    context = {
        'palavras': Words.objects.all
    }
    return render(request, 'index.html', context)

See that the model Words is used when oriented to do a search of all objects related to it.

Your question says: "My model is not being imported", and this can happen. But due to the format of your question, this reply complement is required.

  • The "from" is tiny, just misspelled. I tried to delete the file and redo and it didn’t work either. It’s weird, it’s like Django isn’t considering that my app should be used, as if he hasn’t registered in the project’s Settings.

  • edits the question with the error message you are giving

  • I edited there... but it gives the error "Import: attempted relative import with no known Parent package" it does not find the models of my app. I did another project from scratch to see if it was a bug, gave the same thing.

  • Maybe that "crispy_forms" was not installed in Django , reference: https://django-crispy-forms.readthedocs.io/en/latest/install.html tries to install.

  • Carlos, I found where the problem was. Thanks for your help! Check out my answer above.

  • haha ok... q good q worked

Show 1 more comment

Browser other questions tagged

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