質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

2687閲覧

Questionインスタンスがimportされない

ShotaTsuchido

総合スコア6

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/10/13 13:52

前提・実現したいこと

初めてのDjango アプリ作成4を進めている最中です
https://docs.djangoproject.com/ja/2.2/intro/tutorial04/

発生している問題・エラーメッセージ

Class 'Question' has no 'objects' member Class 'Choice' has no 'DoesNotExist' member

該当のソースコード

イメージ説明
の様にディレクトリが構成されています。

#polls/views.pyのコードの中身
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from django.http import Http404
from django.urls import reverse
from django.shortcuts import get_object_or_404, render
from .models import Choice, Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)

def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})

def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):

return render(request, 'polls/detail.html', { 'question': question, 'error_message': "You didn't select a choice.", }) else: selected_choice.votes += 1 selected_choice.save() return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/results.html', {'question': question})

#polls/models.pyのソースコードの中身

import datetime
from django.db import models
from django.utils import timezone

class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def __str__(self): return self.question_text def was_published_recently(self): return self.pub_date >= timezone.now()

datetime.timedelta(days=1)

class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

def __str__(self): return self.choice_text

試したこと

https://stackoverflow.com/questions/45135263/class-has-no-objects-member
このサイトを参考に、pylint-djangoをインストールし、VS codeのPythonのsettings.jsonで

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

を記述したのですが、どこに書いても
イメージ説明
このように、エラーが出てしまいます。

補足情報(FW/ツールのバージョンなど)

Surface Laptop2
Windows 1809
Python 3.7.4
Django 2.2.6
VS codeは最新バージョン

よろしくお願いします

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

コマンドラインで使っているPythonの実行環境と、VS codeで使用しているPythonの実行環境が違うということを知らずに、pylint-djangoをコマンドラインの方ではインストールしてましたが、VSの方ではインストールしていないことが原因でした。

もし、同じ問題に直面している方はこのサイトが役に立つと思いますので、ぜひ見てみてください
https://www.atmarkit.co.jp/ait/articles/1711/24/news034.html

投稿2019/10/14 05:35

ShotaTsuchido

総合スコア6

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問