[django] django orm

dev stefanCho·2021년 8월 30일
0

django

목록 보기
1/1

django orm 을 간단히 정리해본다.
django orm은 sql을 python에서 쉽게 사용하기 위한 object이다.

아래와 같이 간단한 models.py가 있다면,

class Article(models.Model):
	title = models.CharField(max_length=10)
    date = models.DateTimeField(auto_now_add=True)
    
def __str__(self): # object를 string으로 바꿔주는 내장 method
	return self.title
from articles.models import Article
Article # 경로가 나옴
Article.objects.all() # 모든 QuerySet을 가져옴
article = Article() # instance 생성
article.title = "hello world!" # instance의 title field에 저장
article.save() # db에 저장
Article.objects.all()[0].title # 첫번째 instance의 title을 가져옴
profile
Front-end Developer

0개의 댓글