28 lines
777 B
Python
28 lines
777 B
Python
from rest_framework import serializers
|
|
from apps.product import models as product_models
|
|
|
|
|
|
class ReferenceProductSerializer(serializers.ModelSerializer):
|
|
""" Serializer of reference product """
|
|
|
|
class Meta:
|
|
model = product_models.ReferenceProduct
|
|
fields = '__all__'
|
|
|
|
|
|
class ProductSerializer(serializers.ModelSerializer):
|
|
""" Serializer of product """
|
|
|
|
class Meta:
|
|
model = product_models.Product
|
|
fields = '__all__'
|
|
|
|
def to_representation(self, instance):
|
|
""" Custom output of product serializer """
|
|
|
|
representation = super().to_representation(instance)
|
|
if instance.reference:
|
|
representation['reference'] = ReferenceProductSerializer(instance.reference).data
|
|
|
|
return representation
|