1
0

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.

Django Girls Tutorialを復習する(2) 〜Djangoモデル〜

Last updated at Posted at 2019-07-05

注意事項はこちら。
https://qiita.com/K-Kazutaka/items/1a432bda4f723e6757d6
写経用の記事です。

~$ python manage.py startapp blog
 #新しいアプリケーションの作成
 #アプリケーションをを作ったら、Djangoにそれを使うように伝えないといけません。↓↓に続く。
 

mysite/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
]
blog/models.py
from django.db import models
from django.utils import timezone

class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(
            default=timezone.now)
    published_date = models.DateTimeField(
            blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

  Djangoドキュメント:モデルのフィールド・定義のやり方について (https://docs.djangoproject.com/ja/2.0/ref/models/fields/#field-types)

~$ python manage.py makemigrations blog
 #データベースにモデルのためのテーブルを作成する

~$ python manage.py migrate blog
 #Djangoが作ってくれた移行ファイルをデータベースに追加する


ここで行っていること。

・オブジェクトとは何か?の解説
・DjangoのデフォルトのデータベースはSQLiteです
・blogディレクトリの作成
・ブログポストモデルの作成(blog/models.pyにて)

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?