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

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

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

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

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Python 3.x

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

Q&A

1回答

2714閲覧

AttributeErrorについて

ingramzero

総合スコア16

Django

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

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Python 3.x

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

0グッド

0クリップ

投稿2019/07/22 04:45

編集2019/07/22 05:08

Djangoまとめを参考にしてdjangoのAPIを叩くところまで進んだのですが、q.choice_set.all()というコマンドを叩いたら↓↓というエラーが吐かれました。どうかご助力お願い出来ますでしょうか。

Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: 'Question' object has no attribute 'choice_set'

コマンドプロンプト

>>> from polls.models import Choice, Question # Make sure our __str__() addition worked. >>> Question.objects.all() <QuerySet [<Question: What's up?>]> # Django provides a rich database lookup API that's entirely driven by # keyword arguments. >>> Question.objects.filter(id=1) <QuerySet [<Question: What's up?>]> >>> Question.objects.filter(question_text__startswith='What') <QuerySet [<Question: What's up?>]> # Get the question that was published this year. >>> from django.utils import timezone >>> current_year = timezone.now().year >>> Question.objects.get(pub_date__year=current_year) <Question: What's up?> # Request an ID that doesn't exist, this will raise an exception. >>> Question.objects.get(id=2) Traceback (most recent call last): ... DoesNotExist: Question matching query does not exist. # Lookup by a primary key is the most common case, so Django provides a # shortcut for primary-key exact lookups. # The following is identical to Question.objects.get(id=1). >>> Question.objects.get(pk=1) <Question: What's up?> # Make sure our custom method worked. >>> q = Question.objects.get(pk=1) >>> q.was_published_recently() True # Give the Question a couple of Choices. The create call constructs a new # Choice object, does the INSERT statement, adds the choice to the set # of available choices and returns the new Choice object. Django creates # a set to hold the "other side" of a ForeignKey relation # (e.g. a question's choice) which can be accessed via the API. >>> q = Question.objects.get(pk=1) Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: 'Question' object has no attribute 'choice_set' >>> # Display any choices from the related ob

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: 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 # Create your models here.

polls/admin.py

from django.contrib import admin from .models import Question admin.site.register(Question) # Register your models here.

todo/models.py

from django.db import models class Post(models.Model): body = models.CharField(max_length=200) # Create your models here.

todo/admin.py

from django.contrib import admin from .models import Question admin.site.register(Question) # Register your models here.

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

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

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

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

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

m.ts10806

2019/07/22 04:59

Questionはテーブルでしょうか? テーブル定義やサンプルデータがわかる情報をご提示ください。
guest

回答1

0

'Question' オブジェクトには'choice_set'というアトリビュートはありません
というエラーですね。
どこにそれは書かれてるんでしょう

投稿2019/07/22 05:27

y_waiwai

総合スコア87774

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

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

ingramzero

2019/07/22 05:56 編集

'Question'オブジェクトには確かに'choice_set'というアトリビュートは定義されていませんでした.... ただ、<Query Set[]>の値が返ってきていました。 ディレクトリもディレクトリの中身も何度も確認しましたが参考サイトとの差異はありませんでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問