Python_WEB/Try_Django
[Django]Associate Blog Post to a User with Foreign Keys
AnKiWoong
2020. 6. 21. 14:40
반응형
CodingEntrepreneurs Django 강의 정리
AUTH_USER_MODEL>
1. 사용자를 나타내는 데 사용할 모델
2. 기본 사용자 모델을 오버라이드
ForeignKey>
1. 다대일 관계.
2. 두개의 위치 인수, 즉 모델과 관련된 클래스와 on_delete옵션이 필요
models>
from django.conf import settings
from django.db import models
# Create your models here.
User = settings.AUTH_USER_MODEL
class BlogPost(models.Model): # blogpost_set > queryset
# id = models.IntegerField() # pk
user = models.ForeignKey(User, default=1, null=True, on_delete=models.SET_NULL)
title = models.CharField(max_length=120)
slug = models.SlugField(unique=True) # hello world > hello-world
content = models.TextField(null=True, blank=True)
반응형