Sam
Sam

Reputation: 388

class has no 'objects' member

so im new in django and im trying to make a small market. i made a product app. this is the inside codes: this is for views.py:

from django.shortcuts import render
from django.http import HttpResponse
from products.models import product


def index(request):
  Products = product.objects.all()
  return render(request, 'index.html', {'products': Products})

def new(request):
  return HttpResponse('New Product')

this is for models.py:

from django.db import models


class product(models.Model):
  name = models.CharField(max_length=150)
  price = models.FloatField()
  stock = models.IntegerField()
  image_url = models.CharField(max_length=2083)

i also made a template folder and put this in it for experiment:

<h1>Products</h1>
<ul>
  {% for product in Products %}
    <li>{{ product.name }}</li>
  {% endfor %}
</ul>

and some other usual codes. but i get a pylint error for this part:

product.objects.all()

please help me! thanks

Upvotes: 6

Views: 9003

Answers (5)

Gregory Nitch
Gregory Nitch

Reputation: 11

Working off of PRABHAT SINGH RAJPUT's answer I had to do this:

  1. Add the following to VSCode's settings.json:
"pylint.args": [
        "--load-plugins=pylint_django"
    ],
  1. Add the following to my .vscode folder settings.json for the working environment:
    "pylint.args": [
        "--django-settings-module=<yourprojecthere>.settings",
    ]

Without the addition of setting the django-settings-module I was getting a pylint error Django was not configured.

Note: be sure to replace <yourprojecthere> with the name of your django project.

Information on configuration found in this SO post.

Upvotes: 0

Dan C
Dan C

Reputation: 1

There is another option: ~/.pylintrc and edit the line which says:

load-plugins=

and add django plugin in there:

load-plugins=pylint_django

which of course needs to be installed first:

python3 -m pip install pylint_django

Upvotes: 0

Bhavya Parikh
Bhavya Parikh

Reputation: 3400

Those who are getting errors after adding into the settings.json file add

enter code here
"python.linting.pylintArgs": [
    "--load-plugins=pylint_django",
    "--errors-only" 
],

Upvotes: 1

Olerdrive
Olerdrive

Reputation: 323

That is because PyLint doesn't know anything about Django metaclasses which provide objects attribute. Anyway your E1101 error is just a warning and you can disable it or use special pylint-django plugin to make PyLint aware of magic that Django does.

Another problem of your code is an incorrect usage of context passed to the render call:

return render(request, 'index.html', {'products': Products})

Context is a python dictionary object in which value will be accessible in the template via key. You are passing your queryset via products key, but iterate over Products (mind the first capital letter) key in your template which is not set so your template will not render anything.

Upvotes: 2

Prabhat Singh
Prabhat Singh

Reputation: 1555

Try with this Use pylint --generated-members=objects

Install Django pylint:

pip install pylint-django

ctrl+shift+p > Preferences: Configure Language Specific Settings > Python

The settings.json available for python language should look like the below:

{
    "python.linting.pylintArgs": [
        "--load-plugins=pylint_django"
    ],

    "[python]": {

    }
}

Upvotes: 21

Related Questions