ORM🏹: 장고 ORM 뿌시기

정은경·2020년 8월 12일
0

🎸 Play the Django 

목록 보기
5/57

관계를 나타내는 장고 필드

장고 ORM 룩업

포린키가있는모델클래스.objects.select_related('포린키필드명', '포린키필드명').filter(포린키가바라보든모델클래스__포린키가바라보는모델클래스의필드명=조건)

>>> ArtworkPrice.objects.select_related('artwork','device').filter(artwork__is_customed=False)
<QuerySet [<ArtworkPrice: ArtworkPrice object (2)>, <ArtworkPrice: ArtworkPrice object (3)>, <ArtworkPrice: ArtworkPrice object (4)>, <ArtworkPrice: ArtworkPrice object (5)>, <ArtworkPrice: ArtworkPrice object (6)>, <ArtworkPrice: ArtworkPrice object (7)>, <ArtworkPrice: ArtworkPrice object (8)>, <ArtworkPrice: ArtworkPrice object (9)>, <ArtworkPrice: ArtworkPrice object (10)>, <ArtworkPrice: ArtworkPrice object (11)>, <ArtworkPrice: ArtworkPrice object (12)>, <ArtworkPrice: ArtworkPrice object (13)>, <ArtworkPrice: ArtworkPrice object (14)>, <ArtworkPrice: ArtworkPrice object (15)>, <ArtworkPrice: ArtworkPrice object (16)>, <ArtworkPrice: ArtworkPrice object (17)>, <ArtworkPrice: ArtworkPrice object (18)>, <ArtworkPrice: ArtworkPrice object (19)>, <ArtworkPrice: ArtworkPrice object (20)>, <ArtworkPrice: ArtworkPrice object (21)>, '...(remaining elements truncated)...']>
>>> artwork = ArtworkPrice.objects.select_related('artwork','device').filter(artwork__is_customed=False)
>>> for i in artwork:
...     print(i.price)
...
50.00
50.00
50.00
50.00
50.00

<>

ArtworkPrice.objects.select_related('artwork','device').filter(artwork__is_customed=False)
<QuerySet [<ArtworkPrice: ArtworkPrice object (2)>, <ArtworkPrice: ArtworkPrice object (3)>, <ArtworkPrice: ArtworkPrice object (4)>, <ArtworkPrice: ArtworkPrice object (5)>, <ArtworkPrice: ArtworkPrice object (6)>, <ArtworkPrice: ArtworkPrice object (7)>, <ArtworkPrice: ArtworkPrice object (8)>, <ArtworkPrice: ArtworkPrice object (9)>, <ArtworkPrice: ArtworkPrice object (10)>, <ArtworkPrice: ArtworkPrice object (11)>, <ArtworkPrice: ArtworkPrice object (12)>, <ArtworkPrice: ArtworkPrice object (13)>, <ArtworkPrice: ArtworkPrice object (14)>, <ArtworkPrice: ArtworkPrice object (15)>, <ArtworkPrice: ArtworkPrice object (16)>, <ArtworkPrice: ArtworkPrice object (17)>, <ArtworkPrice: ArtworkPrice object (18)>, <ArtworkPrice: ArtworkPrice object (19)>, <ArtworkPrice: ArtworkPrice object (20)>, <ArtworkPrice: ArtworkPrice object (21)>, '...(remaining elements truncated)...']>
>>> artwork = ArtworkPrice.objects.select_related('artwork','device').filter(artwork__is_customed=False)
>>> for i in artwork:
  print(i.price, i.artwork.id, i.artwork.artwork_type.name, i.artwork.regularartworkimage_set.filter(artwork_id=i.artwork.id)[0].image_1)
  
   중!간!생!략!
50.00 34 Impact Case https://cdn-image02.casetify.com/usr/16401/2626401/~v96/4696941_iphone11-pro-max__color_midnight-green_16000091.png.350x350-w.m80.jpg
50.00 35 Impact Case https://cdn-image02.casetify.com/usr/9516/2769516/~v14/6149093_iphone11-pro-max__color_midnight-green_16000091.png.350x350-w.m80.jpg
50.00 36 Impact Case https://cdn-image02.casetify.com/usr/18017/1518017/~v50/4750641_iphone11-pro-max__color_midnight-green_16000091.png.350x350-w.m80.jpg
50.00 37 Impact Case https://cdn-image02.casetify.com/usr/19008/889008/~v71/4043707_iphone11-pro-max__color_midnight-green_16000091.png.350x350-w.m80.jpg
50.00 38 Impact Case https://cdn-image02.casetify.com/usr/11785/3671785/~v28/5458332_iphone11-pro-max__color_midnight-green_16000091.png.350x350-w.m80.jpg
50.00 39 Impact Case https://cdn-image02.casetify.com/usr/11962/2531962/~v66/4710907_iphone11-pro-max__color_midnight-green_16000091.png.350x350-w.m80.jpg
50.00 40 Impact Case https://cdn-image02.casetify.com/usr/28613/1288613/~v24/4271724_iphone11-pro-max__color_midnight-green_16000091.png.350x350-w.m80.jpg
50.00 41 Impact Case https://cdn-image02.casetify.com/usr/11785/3671785/~v28/5447617_iphone11-pro-max__color_midnight-green_16000091.png.350x350-w.m80.jpg
>>>
for i in artwork:
print(i.artwork.regularartworkimage_set.filter(artwork_id=i.artwork.id).values('artwork_color__name','artwork_color__info'))

중!간!생!략!
ork_color__info': 'radial-gradient(#FF7 0%, #fff 48%, pink 52%, pink 100%) '}, {'artwork_color__name': 'Pink/Blue', 'artwork_color__info': 'https://cdn-stamplib.casetify.com/cms/image/9457634f344728ab9fec8735b52590a7.png'}, {'artwork_color__name': 'Pink/Blue', 'artwork_color__info': 'https://cdn-stamplib.casetify.com/cms/image/9457634f344728ab9fec8735b52590a7.png'}, {'artwork_color__name': 'Pink/Blue', 'artwork_color__info': 'https://cdn-stamplib.casetify.com/cms/image/9457634f344728ab9fec8735b52590a7.png'}, {'artwork_color__name': 'Pink/Blue', 'artwork_color__info': 'https://cdn-stamplib.casetify.com/cms/image/9457634f344728ab9fec8735b52590a7.png'}, {'artwork_color__name': 'Neon Yellow', 'artwork_color__info': '#eaf850'}, {'artwork_color__name': 'Neon Yellow', 'artwork_color__info': '#eaf850'}, {'artwork_color__name': 'Neon Yellow', 'artwork_color__info': '#eaf850'}, {'artwork_color__name': 'Neon Yellow', 'artwork_color__info': '#eaf850'}, '...(remaining elements truncated)...']>
>>>

Foreign Key

on_delete

  • on_delete=models.CASCADE
  • on_delete=models.RESTRICT
  • on_delete=models.SET_NULL = True
  • on_delete=models.PROTECT

Reference

profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글