9
9
from rest_framework .fields import ReadOnlyField
10
10
from rest_framework .utils .serializer_helpers import ReturnDict
11
11
12
+ from apps .category .models import Style
13
+ from apps .like .models import Like
12
14
from apps .product .models import Product , ProductImage , RentalHistory
13
15
from apps .user .serializers import UserInfoSerializer
14
16
@@ -35,6 +37,7 @@ class ProductSerializer(serializers.ModelSerializer[Product]):
35
37
# rental_history = RentalHistorySerializer(many=True, read_only=True)
36
38
# images = serializers.SerializerMethodField()
37
39
images = ProductImageSerializer (many = True , read_only = True )
40
+ is_liked = serializers .SerializerMethodField ()
38
41
39
42
class Meta :
40
43
model = Product
@@ -52,21 +55,42 @@ class Meta:
52
55
"size" ,
53
56
"views" ,
54
57
"product_category" ,
58
+ "styles" ,
55
59
"status" ,
56
60
"amount" ,
57
61
"region" ,
58
62
"created_at" ,
59
63
"updated_at" ,
60
64
"images" ,
61
65
"likes" ,
66
+ "is_liked" ,
62
67
# "rental_history",
63
68
)
64
- read_only_fields = ("created_at" , "updated_at" , "views" , "lender" , "status" , "likes" )
69
+ read_only_fields = ("created_at" , "updated_at" , "views" , "lender" , "status" , "likes" , "is_liked" )
70
+
71
+ def get_is_liked (self , obj : Product ) -> bool :
72
+ user = self .context ["request" ].user
73
+ if user .is_authenticated :
74
+ return Like .objects .filter (user = user , product = obj ).exists ()
75
+ return False
76
+
77
+ def set_styles (self , styles_data : list [Style ]) -> list [Style ]:
78
+ styles = []
79
+ for style in styles_data :
80
+ style_name = style .name
81
+ style_item , _ = Style .objects .get_or_create (name = style_name )
82
+ styles .append (style )
83
+ return styles
65
84
66
85
@transaction .atomic
67
86
def create (self , validated_data : Any ) -> Product :
68
87
image_set = self .context ["request" ].FILES .getlist ("image" )
88
+ styles_data = validated_data .pop ("styles" , [])
69
89
product = Product .objects .create (** validated_data )
90
+
91
+ styles = self .set_styles (styles_data )
92
+ product .styles .set (styles )
93
+
70
94
if image_set :
71
95
product_images = [ProductImage (product = product , image = image ) for image in image_set ]
72
96
ProductImage .objects .bulk_create (product_images )
@@ -77,6 +101,7 @@ def update(self, instance: Product, validated_data: Any) -> Product:
77
101
request = self .context ["request" ]
78
102
received_new_images = request .FILES .getlist ("image" )
79
103
received_existing_images = request .POST .getlist ("image" )
104
+ styles_data = validated_data .pop ("styles" , [])
80
105
81
106
# 기존 이미지와 받은 이미지 id 비교해서 다시 안 온 이미지 삭제
82
107
existing_images = {img .get_image_url (): img .id for img in instance .images .all ()}
@@ -95,4 +120,8 @@ def update(self, instance: Product, validated_data: Any) -> Product:
95
120
for attr , value in validated_data .items ():
96
121
setattr (instance , attr , value )
97
122
instance .save ()
123
+
124
+ # styles 태그 등록
125
+ styles = self .set_styles (styles_data )
126
+ instance .styles .set (styles )
98
127
return instance
0 commit comments