본문 바로가기

Python_WEB/Try_Django

[Django]Associate Blog Post to a User with Foreign Keys

반응형

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)
반응형

'Python_WEB > Try_Django' 카테고리의 다른 글

[Django]Update View with Model Form  (0) 2020.06.21
[Django]Logged In user & Forms  (0) 2020.06.21
[Django]Login Required  (0) 2020.06.21
[Django]Validate Data on Fields  (0) 2020.06.21
[Django]Model Form  (0) 2020.06.21