Skip to content

Swapno963/Advance-Serializer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Advance-Serializers

Done

  1. Work with nested searilizer
  2. Work with nested selrilizer
  3. Serializer Inheritance
  • Basic Inheritance
  1. Custom to_representation and to_internal_value Method
  2. Field-Level Custom Validation
  3. Serializer Relations
  4. Using SerializerMethodField
  5. Integrating Serializers with ViewSets and QuerySets
  • Use multiple serializers for different actions:
class ProductViewSet(viewsets.ModelViewSet):
    queryset = Product.objects.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return ListProductSerializer
        if self.action == 'retrieve':
            return DetailedProductSerializer
        return DefaultProductSerializer

Pending

  1. Custom create and update Methods

  2. Dynamic Fields Serializer

  3. Serializer-Level Validation Use specific fields for relationships:

  • PrimaryKeyRelatedField
  • SlugRelatedField
  • HyperlinkedRelatedField
  • StringRelatedField
  1. Polymorphic Serializers Leverage libraries like django-rest-framework-json-schema or drf-flex-fields for JSON schema validation and dynamic field selection.
  2. Custom Fields
  • Create custom fields for non-standard data types or formats.
class CommaSeparatedListField(serializers.Field):
    def to_representation(self, value):
        return ",".join(value)

    def to_internal_value(self, data):
        return data.split(",")

class CustomSerializer(serializers.Serializer):
    tags = CommaSeparatedListField()
  1. Output Formats (JSON, XML, etc.)
  • Customize output by overriding renderers in the view.
from rest_framework.renderers import JSONRenderer, XMLRenderer

class CustomViewSet(viewsets.ModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    renderer_classes = [JSONRenderer, XMLRenderer]

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages