클래스에 개체 구성원이 없습니다.
def index(request):
latest_question_list = Question.objects.all().order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {'latest_question_list':latest_question_list}
return HttpResponse(template.render(context, request))
해당 함수의 첫 번째 줄에 Question.objects.all ()-> E1101 : Class 'Question has no objects
member '오류가 발생합니다.
Django 문서 자습서를 따르고 있으며 동일한 코드가 실행되고 있습니다.
인스턴스를 호출 해 보았습니다.
Question = new Question()
and using MyModel.objects.all()
또한 그 클래스에 대한 내 models.py 코드는 이것입니다 ...
class Question(models.Model):
question_text = models.CharField(max_length = 200)
pub_date = models.DateTimeField('date published')
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
def __str__(self):
return self.question_text
아무 소용이 없지만 여전히이 오류가 있습니다.
필 린트에 대해 읽고 이것을 실행했습니다 ...
pylint --load-plugins pylint_django
도움이되지 않았습니다. github readme 파일에도 ...
Model.objects 또는 Views.request와 같은 Django 생성 속성에 대한 경고를 방지합니다.
내 virtualenv 내에서 명령을 실행했지만 아무것도 실행하지 않았습니다.
그래서 어떤 도움이라도 좋을 것입니다
설치를 pylint-django
사용하여 pip
다음과 같이
pip install pylint-django
그런 다음 Visual Studio Code에서 다음으로 이동합니다. 사용자 설정 ( Ctrl+ ,또는 파일> 기본 설정> 사용 가능한 경우 설정) 다음을 입력합니다 (VSC의 사용자 지정 사용자 설정에 필요한 중괄호에 유의하십시오).
{"python.linting.pylintArgs": [
"--load-plugins=pylint_django"
],}
@ tieuminh2510 대답은 완벽합니다. 그러나 최신 버전의 VSC에서는 사용자 설정 에서 해당 명령을 편집하거나 붙여 넣는 옵션을 찾을 수 없습니다 . 이제 최신 버전에서 해당 코드 를 추가하려면 다음 단계를 따르십시오 .
보도 CTR + SFT + P는 상기 열려면 명령 팔레트 . 이제 명령 팔레트 유형 Preferences : Configure Language Specific Settings . 이제 Python을 선택 합니다. 여기 오른쪽에이 코드를 붙여 넣으세요.
"python.linting.pylintArgs": [
"--load-plugins=pylint_django",
]
첫 번째 중괄호 안에. 있는지 확인 pylint - 장고를 .
이것이 도움이되기를 바랍니다!
여기에 답이 있습니다. 내 reddit 게시물에서 가져 왔습니다 ... https://www.reddit.com/r/django/comments/6nq0bq/class_question_has_no_objects_member/
이는 오류가 아니라 VSC의 경고 일뿐입니다. Django는이 속성을 모든 모델 클래스에 동적으로 추가하므로 (내부적으로 많은 마법을 사용함) IDE는 클래스 선언을보고 그것에 대해 알지 못하므로 가능한 오류에 대해 경고합니다 (그렇지 않습니다). 개체는 사실 DB 쿼리에 도움이되는 Manager 인스턴스입니다. 이 경고를 정말로 없애고 싶다면 모든 모델로 가서 objects = models.Manager ()를 추가 할 수 있습니다. 이제 VSC는 선언 된 객체를보고 그것에 대해 다시 불평하지 않을 것입니다.
Visual Studio Code 용 Python 확장 용 linter를 변경할 수 있습니다.
VS에서 명령 팔레트 Ctrl + Shift + P를 열고 다음 명령 중 하나를 입력합니다.
Python : Linter 선택
린터를 선택하면 설치됩니다. flake8을 시도했는데 문제가 해결 된 것 같습니다.
가능한 모든 솔루션을 시도했지만 불행히도 내 vscode 설정은 linter 경로를 변경하지 않습니다. 그래서 settings> User Settings> python 에서 vscode 설정을 탐색 해 보았습니다 . Linting : Pylint Path를 찾아 "pylint_django"로 변경합니다. 설정> 사용자 설정> 파이썬 구성 에서 linter를 "pylint_django"로 변경하는 것을 잊지 마십시오. "pyLint"에서 "pylint_django"로.
First install pylint-django using following command
$ pip install pylint-django
Then run the second command as follows:
$ pylint test_file.py --load-plugins pylint_django
--load-plugins pylint_django is necessary for correctly review a code of django
By doing Question = new Question()
(I assume the new
is a typo) you are overwriting the Question model with an intance of Question
. Like Sayse said in the comments: don't use the same name for your variable as the name of the model. So change it to something like my_question = Question()
.
How about suppressing errors on each line specific to each error?
Something like this: https://pylint.readthedocs.io/en/latest/user_guide/message-control.html
Error: [pylint] Class 'class_name' has no 'member_name' member It can be suppressed on that line by:
# pylint: disable=no-member
linter를 -flake8로 변경 하면 문제가 사라집니다.
Django pylint를 설치합니다.
pip install pylint-django
ctrl + shift + p> 기본 설정 : 언어 별 설정 구성> Python
Python 언어에 사용할 수있는 settings.json은 다음과 같습니다.
{
"python.linting.pylintArgs": [
"--load-plugins=pylint_django"
],
"[python]": {
}
}
@ Mallory-Erik이 말한 것에 덧붙여서 : objects = models.Manager()
모달에 넣을 수 있습니다 :
class Question(models.Model):
# ...
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
# ...
def __str__(self):
return self.question_text
question_text = models.CharField(max_length = 200)
pub_date = models.DateTimeField('date published')
objects = models.Manager()
Python 3을 사용하는 경우
python3 -m pip install pylint-django
파이썬 <3 인 경우
python -m pip install pylint-django==0.11.1
참고 : 버전 2.0에는 더 이상 Python 2를 지원하지 않는 pylint> = 2.0이 필요합니다! ( https://pypi.org/project/pylint-django/ )
참고 URL : https://stackoverflow.com/questions/45135263/class-has-no-objects-member
'developer tip' 카테고리의 다른 글
"cordova build android"실행-android : fontVariationSettings 및 android : ttcIndex 속성을 찾을 수 없습니다. (0) | 2020.08.14 |
---|---|
Windows Forms의 프롬프트 대화 상자 (0) | 2020.08.14 |
에뮬레이션과 시뮬레이션의 차이점은 무엇입니까? (0) | 2020.08.14 |
compare ()와 compareTo ()의 차이점은 무엇입니까? (0) | 2020.08.13 |
루비 연산자 "= ~" (0) | 2020.08.13 |