Search code examples
djangodjango-modelsdjango-viewsdjango-querysetdjango-cms

Querying works on shell but not on the Django program code


I'm a complete novice to python and Django.

I have tried several solutions I found on stack overflow, but I still get the same issue. I've tried querying from shell and it works well, but not on my code.

I've installed pylint using

pip install pylint-django

I have also changed the Linter settings on Settings > User Settings > Python from pyLint to pylint_django and also to flake8, but no positive results. I still get the message "Class Courses has no 'objects' member pylint(no-member)"

These are my codes from models.py:

class Courses(models.Model):
course_title = models.CharField(max_length=200)
course_image = models.ImageField(upload_to='course_images/')
course_duration = models.TimeField() 

 def __str__(self):
 return self.course_title

The views.py looks like this:

from django.shortcuts import render
from django.http import HttpResponse
from .models import Courses

def homepage(request):
    cos = Courses.objects.all()
    context={ 'courses': cos }

    return render(request, "main/home.html", context) 

I need help. I'm completely stuck.


Solution

  • It was right under my nose the whole time.

    Goto settings > Search for "terminal" > open any of the do "Edit in settings.json" and add the code below.

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

    NOTE: make sure you add a comma at the end of the last line before adding the code below, else you'd get an error.

    Save that and then go back to settings and search for "lint" check the left hand side of the search and locate Python Configuration

    Make sure "The Linter to use" is set either to pyLint or pylint_django

    That should work fine.