現在Writing your first Django app, part 2を進めています。
そのなかで$ python manage.py shell
と入力してshellで遊ぼうという部分があるのですが、c.question
と入力するとAttributeError
が出ます。
該当コード
python
1# models.py 2import datetime 3 4from django.db import models 5from django.utils import timezone 6 7class Question(models.Model): 8 question_text = models.CharField(max_length=200) 9 pub_date = models.DateTimeField('date published') 10 def __str__(self): 11 return self.question_text 12 def was_published_recently(self): 13 return self.pub_date >= timezone.now() - datetime.timedelta(days=1) 14 15class Choice(models.Model): 16 question = models.ForeignKey(Question, on_delete=models.CASCADE) 17 choice_text = models.CharField(max_length=200) 18 votes = models.IntegerField(default=0) 19 def __str__(self): 20 return self.choice_text
該当エラー
In [17]: c.question --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-17-1b77f6916f78> in <module> ----> 1 c.question AttributeError: 'Choice' object has no attribute 'question'
開発環境
- python3.6.6
- Django2.1.2
試したこと
stackoverflowで同じエラーの相談がありましたが、私の事例とは違う気がしたので、手助けには今の所なっていませんorz
よろしくお願いしますm(__)m
質問回答
matsuand様への回答
ayumka@yukiya:~/PycharmProjects/mysite$ python manage.py sqlmigrate polls 0001 Traceback (most recent call last): File "manage.py", line 8, in <module> from django.core.management import execute_from_command_line ModuleNotFoundError: No module named 'django' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "manage.py", line 14, in <module> ) from exc ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
できましたー(≧∇≦)b
matsuand様、ありがとうございます!
Djangoがなくなっていたので以下の手順を踏んでc.question
を無事通過しました<3
- もう一度
$ pip install Django
する。 $ python manage.py runserver
でhttp://127.0.0.1:8000/polls/
へのアクセス確認- 一応Writing your first Django app, part 2の
$ python manage.py makemigrations polls
から順々に実行
回答1件
あなたの回答
tips
プレビュー