Model Product in django has no attribute 'objects'

In lesson 41 Mosh called the objects attribute from the Product class, but in my case no such attribute exists (as shown in the picture above).
Could anyone help me?

Thanks in advance,

Jonas

Hi, on the top try:
“from store.models import Product” not what you have there of “from storefront.store.models import Product”

Will it work?

1 Like

Nope,


doesn’t find the Product model then :confused:

Does your file tree look like this? Can you post screenshot

Yes it does

The only difference between us is that I am using PyCharm instead of VS Code.

You have djangoproject as your root folder not storefront. You will see on 5 - Creating your first django project Mosh delete’s root folder


tried it like this but still didn’t work.

Its still the same, the path to your project is PycharmProjects\djangoproject see at the top

I don’t see any underlined errors there, whats the error message or code you are getting?

from django.shortcuts import render
from django.http import HttpResponse
from store.models import Product


def say_hello(request):
    Product.objects  **# <---- error ( it dosen´t find the objects attribute of any model i made)** 
    return render(request, 'hello.html', {'name': 'Jonas'})

my file tree now:

If the issue relates to intellisense or code suggestions, I think it may be an issue with your IDE as opposed to the actual source code. Try the steps below one by one to see if it solves your issue:

  1. Check if the project interpreter is set correctly: Go to “Settings” or “Preferences” in PyCharm (depending on your operating system) and navigate to “Project: [Your Project Name]” > “Python Interpreter.” Make sure that the correct interpreter is selected for your project.

  2. Invalidate caches and restart: Go to “File” > “Invalidate Caches / Restart” and choose “Invalidate and Restart.” This action will clear the caches and restart PyCharm.

  3. Ensure that the necessary packages are installed: Make sure that you have installed all the required packages for your project. You can use the package manager or a virtual environment to manage your packages. Check if the necessary packages are installed and accessible by PyCharm.

  4. Confirm that the file is in a recognized source root: Ensure that the file you are working on is located within a recognized source root of your PyCharm project. PyCharm may not provide IntelliSense for files outside of the recognized source roots.

  5. Enable the “Autocomplete” feature: Check if the “Autocomplete” feature is enabled in your PyCharm settings. Go to “Settings” or “Preferences” > “Editor” > “General” > “Code Completion” and make sure that “Autocomplete” is selected.

  6. Wait for indexing to complete: PyCharm performs indexing in the background to build its code analysis database. If you have recently opened a large project or made significant changes, give PyCharm some time to finish indexing. You can see the indexing progress in the bottom-right corner of the PyCharm window.

  7. Restart PyCharm: If none of the above steps work, try restarting PyCharm to see if it resolves the issue.

You were right. It’s an issue with my IDE because the objects attribute instantly pops up as soon as I tried to find it in Visual Studio Code.
I am going to continue using PyCharm because it still works, I just need to type it in manually and ignore the warning.

Thanks a lot for you help :slight_smile:

1 Like