django C.R.U.D 맛보기

김예랑·2021년 6월 19일
0

django C.R.U.D (Create, Read, Update, Delete)

  • 장고에서 데이터베이스의 테이블에 데이터(row)를 create, read, update, delete 할 수 있다.
    기본적인 method들의 사용법을 정리해보자.

1. create

  • Table.objects.create(column1="value1",....)

  • a = Table(column1="value1",....)
    a.save()

2. read

  • Table.objects.get(column1="value1"..) : 객체로 반환, 무조건 하나의 row만 있을 때 사용
  • Table.objects.filter(column1="value1"..) : 쿼리셋으로 반환, 만족하는 여러개의 row를 반환
  • Table.objects.exclude(column1="value1"..) : 해당 row 제외 후 반환
  • Table.objects.all() : 모든 row 반환
  • Table.objects.values() : dictionary 형태로 반환

3. update

  • Table.objects.filter().update(columnN="valueN"...)

4. delete

  • Table.objects.get().delete()
  • Table.objects.filter().delete()

그 외 exists(), count().. 등 여러가지 method들이 있다.
구글링하면 종류나 사용법은 쉽게 찾을 수 있다.
다양한 method들을 목적에 맞게 찾아서 사용하면 되겠다.

profile
안녕하세요. 백엔드 애송이 백송이 김예랑 입니다.

0개의 댓글