레몬베이스 온보딩 프로젝트

레몬베이스에서 실제 제품 개발에 사용하는 개발 환경 배우기

코스 전체목록

닫기

API 개발 - Part 7

리뷰 서비스 API 개발 - 정책 변경, 삭제 구현

ManyToManyField의 업데이트: set 함수 사용
https://docs.djangoproject.com/en/3.0/ref/models/relations/#django.db.models.fields.related.RelatedManager.set
- clear flag는 set이 업데이트 시 기존 값이 한번에 많이 제거될 가능성이 클 때 True 설정하는 것이 좋을 듯함
add, create, remove, clear, set 함수 호출 시에는 따로 save를 더 호출할 필요 없음

Note that add()create()remove()clear(), and set() all apply database changes immediately for all types of related fields. In other words, there is no need to call save() on either end of the relationship.

If you use prefetch_related(), the add()remove()clear(), and set() methods clear the prefetched cache.

외래키로 묶인 필드의 값을 업데이트 할 때에는 (해당 필드).save()를 따로 해주어야 함
예시:
rc = ReviewCycle.objects.get(pk=policy_id)
rc.name = data['name']
rc.question.title = data['question']['title']
rc.question.description = data['question']['description']
rc.question.save()
rc.save()
rc.reviewees.set(data['reviewees'])

 

댓글

댓글 본문
버전 관리
김시준
현재 버전
선택 버전
graphittie 자세히 보기