django model사용시 one to many , many to one 활용

order / detail order가 있을 때

1
2
3
4
5
6
7
many to one일때
detail_order = DetailOrder.objects.select_related('order').get(id=request.data['id'])

one to many일땐
order = order.objects.filter(id=1).prefetch_related("detail_order")

remain_detail_order = DetailOrder.objects.filter(order__id=detail_order.order.id).count()
Share