from django.db import models
from django.conf import settings
User = settings.AUTH_USER_MODEL
class User(models.Model):
email = models.EmailField(max_length=255, unique=True)
name = models.CharField(max_length=255)
is_regular_member = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Post(models.Model):
title = models.CharField(verbose_name='글 제목', max_length=30)
content = models.TextField(verbose_name='글 내용')
writer = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
User 모델
과 Post 모델
을 정의해줍니다.
models.py
을 수정한 뒤에는 항상 migration 작업을 진행해야 합니다.
$ python manage.py makemigrations
blog/migrations/0001_initial.py
- Create model User
- Create model Post
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying blog.0001_initial... OK