복습

hyeyul·2020년 6월 2일
0

select_related가 쓰이는 경우

  • 정참조에서 쓰이고 onetoone이다
>>> a = Product.objects.select_related('main_category','sub_category','nutrient').get(id=1)
>>> a.main_category.name
'음료'

prefectch_related가 쓰이는 경우

  • manytomany에서 쓰인다
c = Product.objects.prefetch_related('allergen').get(id=1)

c.allergen.all()

d = [{ 'id':i.id ,'name':i.name } for i in c.allergen.all()]

0개의 댓글