1
- # from rest_framework import serializers
2
- # from apps.product.models import Product, ProductImage, ProductCategory, StyleCategory
3
- # from apps.user.models import Account
4
1
import logging
5
2
from typing import Any
6
3
7
4
from django .db import transaction
8
5
from rest_framework import serializers
9
- from rest_framework .fields import ReadOnlyField
10
- from rest_framework .utils .serializer_helpers import ReturnDict
11
6
12
- from apps .category .models import Style
7
+ from apps .category .models import Category , Style
13
8
from apps .like .models import Like
14
9
from apps .product .models import Product , ProductImage , RentalHistory
15
10
from apps .user .serializers import UserInfoSerializer
@@ -32,13 +27,18 @@ class Meta:
32
27
33
28
34
29
class ProductSerializer (serializers .ModelSerializer [Product ]):
35
- # lender = ReadOnlyField(source="lender.nickname")
36
30
lender = UserInfoSerializer (read_only = True )
37
- # rental_history = RentalHistorySerializer(many=True, read_only=True)
38
- # images = serializers.SerializerMethodField()
39
31
images = ProductImageSerializer (many = True , read_only = True )
40
32
is_liked = serializers .SerializerMethodField ()
41
33
34
+ # product_category = serializers.PrimaryKeyRelatedField(queryset=Category.objects.all(), write_only=True)
35
+ # category_name = serializers.ReadOnlyField(source="product_category.name")
36
+ product_category = serializers .SlugRelatedField (slug_field = "name" , queryset = Category .objects .all ())
37
+
38
+ # styles = serializers.PrimaryKeyRelatedField(queryset=Style.objects.all(), many=True, write_only=True)
39
+ # style_names = serializers.SerializerMethodField(read_only=True)
40
+ styles = serializers .SlugRelatedField (many = True , slug_field = "name" , queryset = Style .objects .all ())
41
+
42
42
class Meta :
43
43
model = Product
44
44
fields = (
@@ -55,7 +55,9 @@ class Meta:
55
55
"size" ,
56
56
"views" ,
57
57
"product_category" ,
58
+ # "category_name",
58
59
"styles" ,
60
+ # "style_names",
59
61
"status" ,
60
62
"amount" ,
61
63
"region" ,
@@ -64,10 +66,15 @@ class Meta:
64
66
"images" ,
65
67
"likes" ,
66
68
"is_liked" ,
67
- # "rental_history",
68
69
)
69
70
read_only_fields = ("created_at" , "updated_at" , "views" , "lender" , "status" , "likes" , "is_liked" )
70
71
72
+ # def get_category_name(self, obj: Product) -> str:
73
+ # return obj.product_category.name
74
+
75
+ # def get_style_names(self, obj: Product) -> list[str]:
76
+ # return [style.name for style in obj.styles.all()]
77
+
71
78
def get_is_liked (self , obj : Product ) -> bool :
72
79
user = self .context ["request" ].user
73
80
if user .is_authenticated :
0 commit comments