Djangoおよびプログラム初心者です。試行錯誤をしながらプログラムを作成しています。
スケジュール管理をしたくてスケジュールアプリを作成していますが、管理画面のエラーで困っています。
Djangoでアプリをsettings.pyに追加してmodels.pyを作成しました。migrate とmakemigrationsでもエラーなくすべてOKで終了したにもかかわらず、Django管理画面から項目の追加・変更をしようとボタンをクリックするとこのようなエラーになります。
開発環境:
Python3.7
Django 2.1.4
以下adminでのエラー表示
OperationalError at /admin/schedule/schedule/add/ no such table: student Request Method: GET Request URL: http://127.0.0.1:8000/admin/schedule/schedule/add/ Django Version: 2.1.4 Exception Type: OperationalError Exception Value: no such table: student Exception Location: c:\users\775na\appdata\local\programs\python\python37\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 296 Python Executable: c:\python\practice\Scripts\python.exe Python Version: 3.7.1 Python Path: ['c:\python\practice\project', 'c:\python\practice\Scripts\python37.zip', 'c:\python\practice\DLLs', 'c:\python\practice\lib', 'c:\python\practice\Scripts', 'c:\users\775na\appdata\local\programs\python\python37\Lib', 'c:\users\775na\appdata\local\programs\python\python37\DLLs', 'c:\python\practice', 'c:\python\practice\lib\site-packages', 'c:\users\775na\appdata\local\programs\python\python37', 'c:\users\775na\appdata\local\programs\python\python37\lib\site-packages']
models.pyはこのように作成しました。まだ意味がよくわかっていないのでモデルの作り方が悪いかもしれません。
from django.db import models from django.utils.translation import ugettext_lazy as _ from django.utils import timezone # Create your models here. class User(models.Model): # blank=Falseは入力必須項目 name = models.CharField(_('name'), max_length=30, blank=False) email = models.EmailField(_('email address'), unique=False) country = models.CharField(_('country'), max_length=50, blank=False) class Steady(models.Model): # カテゴリーを作成 steady = models.CharField(_('steady customer'), max_length=255, blank=True) class Meta: db_table = 'steady' def __str__(self): return self.steady class Student(models.Model): # 名前だけ必須項目 name = models.CharField(_('student name'), max_length=30, blank=False) sky_id = models.CharField(_('skype id'), max_length=50, blank=True) # skypeID country = models.CharField(_('country'), max_length=50, blank=True) steady = models.ForeignKey(Steady, on_delete=models.SET_NULL, null=True) created_at = models.DateTimeField(default=timezone.now) # Studentモデルはstudentテーブルと紐づいている IDという主キーも自動で不可されている class Meta: db_table = 'student' class Schedule(models.Model): # 同じフィールドで複数の項目をForeignKeyで参照するときはrelated_nameが必要。これでデータが取れる student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name="student_name") lesson_date = models.DateTimeField(blank=False) article = models.CharField(_('article'), max_length=225, blank=True) detail = models.TextField(_('lesson content')) sky_id = models.ForeignKey(Student, on_delete=models.SET_NULL, null=True, related_name="sky_add") # skypeID created_at = models.DateTimeField(default=timezone.now) class Meta: db_table = 'schedule'
他のアプリを見よう見まねで真似して作成しているので、まだコードの意味がよくわかっていません。
エラーはSteady、Student、Scheduleでエラーになります。
モデルの作成で間違っている部分を教えていただけませんか?
またなぜそれが悪いのかも教えていただければ幸いです。
よろしくお願いいたします。
以下はwebページでのエラー
Error during template rendering In template c:\users\775na\appdata\local\programs\python\python37\lib\site-packages\django\contrib\admin\templates\admin\includes\fieldset.html, error at line 19 no such table: student 9 {% for field in line %} 10 <div{% if not line.fields|length_is:'1' %} class="fieldBox{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}> 11 {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %} 12 {% if field.is_checkbox %} 13 {{ field.field }}{{ field.label_tag }} 14 {% else %} 15 {{ field.label_tag }} 16 {% if field.is_readonly %} 17 <div class="readonly">{{ field.contents }}</div> 18 {% else %} 19 {{ field.field }} ←ここが赤く警告されています 20 {% endif %} 21 {% endif %} 22 {% if field.field.help_text %} 23 <div class="help">{{ field.field.help_text|safe }}</div> 24 {% endif %} 25 </div> 26 {% endfor %} 27 </div> 28 {% endfor %} 29</fieldset>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。