类在Django中没有“对象”成员 [英] Class has no 'objects' member in django
问题描述
from django.http import HttpResponse
from .models import Destination
def index(request):
boards = Destination.objects.all()
boards_names = list()
for Destination in boards:
boards_names.append(Destination.destinationtext)
response_html = '<br>'.join(boards_names)
return HttpResponse(response_html)
我只是为了练习django框架而编写了这段代码,但是我通过pylint遇到以下错误:
I have written this code following just for practice of django framework but I am getting the following errors through pylint :
E1101:Class 'Destination' has no 'objects' member
E0601:Using variable 'Destination' before assignment
推荐答案
您有两个不同的问题,而不仅仅是您所说的一个:
You have two different issues, and not just one as you say:
E1101:类目标没有对象成员
:是由于 pylint
不了解我们的特殊Django变量而发生的警告
。像 pylint-django
这样的pylint插件可能会解决问题。
E1101:Class 'Destination' has no 'objects' member
: Is a warning
that occurs because pylint
doesn't know about our special Django variables. A pylint plugin like pylint-django
might do the trick.
E0601:分配前使用变量 Destination
:在代码的for循环中定义了一个名为目的地
。这不仅是不好的做法,因为python变量必须位于 lowercase_underscore
中,而且它会覆盖 Destination
类,这就是导致此错误的原因。您可能想执行以下操作:
E0601:Using variable 'Destination' before assignment
: In the for loop in your code you defined a variable called Destination
. This is not only bad practice because python variables need to be in lowercase_underscore
but it overrides the Destination
class, and that's what is causing this error. You probably wanted to do something like this:
for d in boards:
# Or:
for destination in boards:
这篇关于类在Django中没有“对象”成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!