28 lines
No EOL
905 B
Python
28 lines
No EOL
905 B
Python
from rest_framework import serializers
|
|
from .models import Cart, CartItem
|
|
from api.serializers import ProductSerializer
|
|
|
|
|
|
class CartItemSerializer(serializers.ModelSerializer):
|
|
product = ProductSerializer(read_only=True)
|
|
product_id = serializers.PrimaryKeyRelatedField(
|
|
queryset=CartItem.objects.all().model._meta.get_field('product').related_model,
|
|
source='product',
|
|
write_only=True
|
|
)
|
|
|
|
class Meta:
|
|
model = CartItem
|
|
fields = ['id', 'product', 'product_id', 'quantity', 'size', 'color', 'added_at']
|
|
|
|
|
|
class CartSerializer(serializers.ModelSerializer):
|
|
items = CartItemSerializer(many=True, read_only=True)
|
|
total = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Cart
|
|
fields = ['id', 'user', 'session_key', 'items', 'total', 'created_at', 'updated_at']
|
|
|
|
def get_total(self, obj):
|
|
return obj.get_total() |