how to add product to my cart in django rest frame work drf

this is my model class Cart(models.Model): id = models.UUIDField(primary_key=True, default=uuid4) created_at = models.DateTimeField(auto_now_add=True) class CartItem(models.Model): cart = models.ForeignKey(Cart, on_delete=models.CASCADE, related_name=’items’) product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name=’cart_items’) quantity = models.PositiveSmallIntegerField() class Meta: unique_together = [[‘cart’, ‘product’]]

this is my views

class CartItemViewSet(ModelViewSet): http_method_names = [‘get’,’post’,’patch’,’delete’] serializer_class = CartItemSerializer queryset = CartItem.objects.all() def get_queryset(self): cart_pk = self.kwargs[‘cart_pk’] return CartItem.objects.select_related(‘product’).filter(cart_id = cart_pk).all() def get_serializer_class(self): if self.request.method == ‘POST’: return AddCartItemSerializer elif self.request.method == ‘PATCH’: return ChangeCartItemSerializer return CartItemSerializer def get_serializer_context(self): return {‘cart_pk’:self.kwargs[‘cart_pk’]} class CartViewSet(CreateModelMixin, RetrieveModelMixin, DestroyModelMixin, GenericViewSet, ): serializer_class = CartSerializer queryset = Cart.objects.prefetch_related(‘items__product’).all() lookup_value_regex = ‘[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}’

and my serializer

class ProductSerializer(serializers.ModelSerializer): title = serializers.CharField(max_length=255, source=’name’) price = serializers.DecimalField(max_digits=6, decimal_places=2,source=’unit_price’) unit_price_after_tax = serializers.SerializerMethodField() class Meta: model = Product fields = [‘id’, ‘title’, ‘price’,’category’, ‘unit_price_after_tax’, ‘inventory’, ‘description’] def create(self, validated_data): product = Product(**validated_data) product.slug = slugify(product.name) product.save() return product # def update(self, instance, validated_data): # instance.inventory = validated_data.get(‘inventory’) # instance.save() # return instance def get_unit_price_after_tax(self , product): return round(product.unit_price * Decimal(1.09), 2) def validate(self, data): if len(data[‘name’]) < 6: raise serializers.ValidationError(‘Product lentgh should be at least 6.’) return data class CartProductSerializer(serializers.ModelSerializer): class Meta: model = Product fields = [‘id’, ‘name’, ‘unit_price’] class ChangeCartItemSerializer(serializers.ModelSerializer): class Meta: model = CartItem fields = [‘quantity’] class AddCartItemSerializer(serializers.ModelSerializer): class Meta: model = CartItem fields = [‘id’, ‘product’, ‘quantity’] def create(self, validated_data): cart_id = self.context[‘cart_pk’] product = validated_data.get(‘product’) quantity=validated_data.get(‘quantity’) try: cart_item = CartItem.objects.get(cart_id=cart_id, product_id=product.id) cart_item.quantity += quantity cart_item.save() except CartItem.DoesNotExist: cart_item = CartItem.objects.create(cart_id=cart_id, **validated_data) return cart_item class CartItemSerializer(serializers.ModelSerializer): product = CartProductSerializer() item_total = serializers.SerializerMethodField() class Meta: model = CartItem fields = [‘id’, ‘product’, ‘quantity’, ‘item_total’] read_only_fields = [‘id’] def get_item_total(self, cart_item): return cart_item.quantity * cart_item.product.unit_price class CartSerializer(serializers.ModelSerializer): items = CartItemSerializer(many=True, read_only = True) total_price = serializers.SerializerMethodField() class Meta: model = Cart fields = [‘id’, ‘items’,’total_price’] read_only_fields = [‘id’, ] def get_total_price(self, cart): return sum([item.quantity * item.product.unit_price for item in cart.items.all()])

can any one help me ???????????????????????????????

i have

i try searching in internet and ask chat gpt but nothing can you help me

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật