0
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 1 year has passed since last update.

Django スタートアップ その2(アプリ作成~データベース作成編)

Last updated at Posted at 2023-02-26

新しいアプリの作成

コマンドライン
python manage.py startapp blog

※新しいアプリを作ったらsettings.pyINSTALLED_APPSにアプリ名を追加する

settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    '(新しいアプリの名前)',新しく追加したアプリ
]

アプリ名/apps.py::(アプリ名)Cofigメソッド内のnameから確認可能

モデルの追加

  • DjangoのDBを担うファイル
  • MVCモデルのM(Model)に該当する
models.py
from django.conf import settings
from django.db import models
from django.utils import timezone
class Post(models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL, 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
コマンドライン
python manage.py makemigrations (アプリ名)
python manage.py migrate (アプリ名)

参考記事

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