我开始使用Django Python框架(按照著名的Mozilla示例项目开发一个Ubuntu 20.04 )。这个:页面
我使用的是Python3.8.5版本。
我发现了以下问题(在堆栈溢出上,我看到很多人都有这个问题,这应该与Pylint有关)。
基本上,这里暴露的问题是相同的:https://stackoverflow.com/questions/49951637/django-visual-studio-tutorial-objects-method-error
我有这个Django views.py文件:
from django.shortcuts import render
# Create your views here.
from .models import Book, Author, BookInstance, Genre
def index(request):
"""View function for home page of site."""
# Generate counts of some of the main objects
num_books = Book.objects.all().count()
num_instances = BookInstance.objects.all().count()
# Available books (status = 'a')
num_instances_available = BookInstance.objects.filter(status__exact='a').count()
# The 'all()' is implied by default.
num_authors = Author.objects.count()
context = {
'num_books': num_books,
'num_instances': num_instances,
'num_instances_available': num_instances_available,
'num_authors': num_authors,
}
# Render the HTML template index.html with the data in the context variable
return render(request, 'index.html', context=context)问题是Visual代码给出了以下在线错误,如下所示:
num_books = Book.objects.all().count()错误是:
类“Book”没有“对象”成员(非成员)
这是我的图书类代码:
class Book(models.Model):
"""Model representing a book (but not a specific copy of a book)."""
title = models.CharField(max_length=200)
author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
# Foreign Key used because book can only have one author, but authors can have multiple books
# Author as a string rather than object because it hasn't been declared yet in file.
summary = models.TextField(max_length=1000, help_text="Enter a brief description of the book")
isbn = models.CharField('ISBN', max_length=13,
unique=True,
help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn'
'">ISBN number</a>')
genre = models.ManyToManyField(Genre, help_text="Select a genre for this book")
# ManyToManyField used because a genre can contain many books and a Book can cover many genres.
# Genre class has already been defined so we can specify the object above.
language = models.ForeignKey('Language', on_delete=models.SET_NULL, null=True)
class Meta:
ordering = ['title', 'author']
def display_genre(self):
"""Creates a string for the Genre. This is required to display genre in Admin."""
return ', '.join([genre.name for genre in self.genre.all()[:3]])
display_genre.short_description = 'Genre'
def get_absolute_url(self):
"""Returns the url to access a particular book instance."""
return reverse('book-detail', args=[str(self.id)])
def __str__(self):
"""String for representing the Model object."""
return self.title我的代码工作正常,因此错误与我的代码无关,但它是一个lint错误。
我试着遵循这个链接中指出的步骤:https://stackoverflow.com/questions/49951637/django-visual-studio-tutorial-objects-method-error
但这是行不通的。我的IDE中仍然有这个错误。
我是这样做的:
我在全局环境和我的项目使用的虚拟环境中安装了这些包。
我用这样的方式编辑它:
{
"python.pythonPath": "/home/andrea/Documenti/Python-WS/Environments/LOCAL-L>
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"[python]": {
},
"python.linting.pylintArgs": [
"--load-plugins", "pylint_django"
],
}然后我重新启动了Visual代码,但它在IDE中仍然存在以下错误:

我的设置有什么问题?如何在我的项目中启用Python和Django lint?我遗漏了什么?
此外,是否有一种为所有Python项目全局启用这个lint功能的方法,以避免为每个项目设置这个lint功能?
发布于 2020-12-22 17:59:43
我自己解决了这个问题,方法如下:
现在它很好用。
https://stackoverflow.com/questions/65408445
复制相似问题