Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags more
Archives
Today
Total
관리 메뉴

JOOHUUN

django 구조 2 / models.py / class 작성 본문

카테고리 없음

django 구조 2 / models.py / class 작성

JOOHUUN 2022. 5. 26. 19:01

1. ORM 이란 

Object Relatinal Mapping의 약자로 데이터베이스를 하나의 Object로 보고 

데이터 베이스를 SQL 언어가 아닌 클래스로 표현 및 사용 가능하게 한다. 

 

1) 클래스의 형태 

from django.db import models  ## user/models.py 

class UserModel(models.Model): # Create your models here.
    class Meta:  # 모델의 정보를 담고 있다 
        db_table = "my_user"  # 테이블의 이름 지정

    # 테이블의 속성들
    username = models.CharField(max_length=20, null=False)
    password = models.CharField(max_length=256, null=False)
    bio = models.CharField(max_length=256, default='')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

 

2. 위에서 만든 User Model을 데이터베이스에 넣는 과정

- 명령어는 터미널 창에 입력

 1) python manage.py makemigrations  # db 변경

 2) python manage.py migrate  # 변경된 db를 적용

 

 

 

 

 

Comments