0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

models.pyの設定方法

Last updated at Posted at 2019-10-27

#内容
djangoでwebサービスを作る際に使用するmodels.pyの記述方法をまとめる。

まずmodels.pyを編集する。
今回は簡単なブログ投稿サービスを想定して、
投稿に紐付く、日付・タイトル・本文をmodelに記載する。

from django.db import models
from django.utils import timezone #djangoではdatetime.nowの代わりにtimezone.nowで現在時刻を取得する

# Create your models here.
class Day(models.Model):
    title = models.CharField('タイトル',max_length=200)
    text = models.TextField('本文')
    date = models.DateTimeField('日付',default=timezone.now)

そしてターミナルでmodels.pyへの変更を読み込む。

python3.7 manage.py makemigrations myblog
Migrations for 'myblog':
  myblog/migrations/0001_initial.py
    - Create model Day

成功した場合このように表示されるので、migrateして反映する。以下のようになれば成功。

python3.7 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, myblog, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK

以上。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?