Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 995
Sam Avatar asked Oct 06 '19 07:10

Sam


3 Answers

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]": {

    }
}
like image 94
PRABHAT SINGH RAJPUT Avatar answered Nov 10 '22 23:11

PRABHAT SINGH RAJPUT


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.

like image 29
Alex Mokeev Avatar answered Nov 10 '22 23:11

Alex Mokeev


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" 
],
like image 30
Bhavya Parikh Avatar answered Nov 10 '22 23:11

Bhavya Parikh