Coverage for src/products/serializers/inventory.py: 80%

15 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-08-04 12:59 +0300

1""" 

2This module contains the serializer for inventory items related to products. 

3 

4It provides: 

5- Serialization of inventory fields such as size, quantity, and price 

6- Logic to expose content type and object ID for generic relations (so inventory can be linked to different product types) 

7- Used to serialize inventory data for product APIs 

8""" 

9 

10from django.contrib.contenttypes.models import ContentType 

11 

12from rest_framework import serializers 

13 

14from src.products.models.inventory import Inventory 

15 

16 

17class InventorySerializer(serializers.ModelSerializer): 

18 content_type = serializers.SerializerMethodField() 

19 object_id = serializers.SerializerMethodField() 

20 

21 class Meta: 

22 model = Inventory 

23 fields = [ 

24 'id', 

25 'size', 

26 'quantity', 

27 'price', 

28 'content_type', 

29 'object_id', 

30 ] 

31 depth = 2 

32 

33 def get_content_type(self, obj): 

34 content_type = ContentType.objects.get_for_model(obj) 

35 

36 return content_type.model 

37 

38 def get_object_id(self, obj): 

39 

40 return obj.pk