Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

django serializer

# serialization in django

from rest_framework import serializers
from .models import Product

class ProductSerializers(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = '__all__'
        
# adding extra fields, that are in models
    def to_representation(self, instance):
      data = super().to_representation(instance)
      data['is_on_sale'] = instance.is_on_sale()
      data['current_price'] = instance.current_price()
      return data
 
PREVIOUS NEXT
Tagged: #django #serializer
ADD COMMENT
Topic
Name
7+8 =