장고 프로젝트 회고 1 - get_or_created편

김민정·2022년 6월 5일
0

장고 프로젝트1

목록 보기
1/4

cart, created = Cart.objects.get_or_create(
                             user = user,
                             option_product = selected_product_id,
                             defaults = {'count' : count },
                            )
if not created:
              cart.count += int(count)      
              cart.save()

1.get_or_created란?
객체가 존재할 경우 객체를 얻고 객체가 존재하지 않으면 생성하는 메소드

2.사용한 이유?
장바구니의 경우 장바구니에 물품이 존재하면 갯수가 더해지고 없으면 물품 자체를 생성하는 것이 필요하기 때문

3.유사한 메소드
3-1) update_or_created로도 사용가능

0개의 댓글