{"id":4349,"library":"django-rest-polymorphic","title":"Django REST Polymorphic","description":"django-rest-polymorphic is a Python library that provides polymorphic serializers for Django REST Framework. It enables you to easily define serializers for models that utilize inheritance through the django-polymorphic library. The current version is 0.1.10, and while its core functionality has been integrated into django-polymorphic, the standalone package is still available on PyPI.","status":"active","version":"0.1.10","language":"en","source_language":"en","source_url":"https://github.com/denisorehovsky/django-rest-polymorphic","tags":["django","rest-framework","drf","polymorphic","serializers","inheritance","api"],"install":[{"cmd":"pip install django-rest-polymorphic","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Required for defining polymorphic models that django-rest-polymorphic serializes.","package":"django-polymorphic","optional":false},{"reason":"It's an extension for Django REST Framework.","package":"djangorestframework","optional":false}],"imports":[{"note":"While this was the original import for django-rest-polymorphic, the core functionality (PolymorphicSerializer) has been incorporated into django-polymorphic itself. The recommended import path for new projects or migrations is now `from polymorphic.contrib.drf.serializers import PolymorphicSerializer`.","wrong":"from polymorphic.contrib.drf.serializers import PolymorphicSerializer","symbol":"PolymorphicSerializer","correct":"from rest_polymorphic.serializers import PolymorphicSerializer"}],"quickstart":{"code":"from django.db import models\nfrom rest_framework import serializers, viewsets\nfrom polymorphic.models import PolymorphicModel\nfrom rest_polymorphic.serializers import PolymorphicSerializer\n\n# 1. Define polymorphic models using django-polymorphic\nclass Project(PolymorphicModel):\n    topic = models.CharField(max_length=30)\n\nclass ArtProject(Project):\n    artist = models.CharField(max_length=30)\n\nclass ResearchProject(Project):\n    supervisor = models.CharField(max_length=30)\n\n# 2. Define serializers for each concrete model\nclass ProjectSerializer(serializers.ModelSerializer):\n    class Meta:\n        model = Project\n        fields = ('topic', 'resource_type') # 'resource_type' is added automatically by PolymorphicSerializer\n\nclass ArtProjectSerializer(serializers.ModelSerializer):\n    class Meta:\n        model = ArtProject\n        fields = ('topic', 'artist', 'resource_type')\n\nclass ResearchProjectSerializer(serializers.ModelSerializer):\n    class Meta:\n        model = ResearchProject\n        fields = ('topic', 'supervisor', 'resource_type')\n\n# 3. Create a PolymorphicSerializer to map models to serializers\nclass ProjectPolymorphicSerializer(PolymorphicSerializer):\n    model_serializer_mapping = {\n        Project: ProjectSerializer,\n        ArtProject: ArtProjectSerializer,\n        ResearchProject: ResearchProjectSerializer\n    }\n\n# 4. Use the polymorphic serializer in a ViewSet\nclass ProjectViewSet(viewsets.ModelViewSet):\n    queryset = Project.objects.all()\n    serializer_class = ProjectPolymorphicSerializer\n","lang":"python","description":"This quickstart demonstrates how to set up polymorphic models using `django-polymorphic`, define individual `ModelSerializer`s for each concrete type, and then create a `PolymorphicSerializer` to handle the mapping. Finally, it shows how to integrate this into a Django REST Framework `ViewSet` to expose a polymorphic API endpoint. You would then include this ViewSet in your Django project's `urls.py`."},"warnings":[{"fix":"Change import statements from `from rest_polymorphic.serializers import PolymorphicSerializer` to `from polymorphic.contrib.drf.serializers import PolymorphicSerializer`.","message":"The core `PolymorphicSerializer` functionality has been incorporated directly into `django-polymorphic` (specifically at `polymorphic.contrib.drf.serializers.PolymorphicSerializer`). While `django-rest-polymorphic` still exists, the recommended approach for new projects or migrating existing ones is to switch imports.","severity":"breaking","affected_versions":"All versions, as this is a migration path recommendation for users of `django-polymorphic`'s built-in DRF support."},{"fix":"Add `extra_kwargs` to your child serializers' `Meta` class, for example: `extra_kwargs = {'url': {'view_name': 'drf:project-detail', 'lookup_field': 'pk'}}`.","message":"When extending `HyperlinkedModelSerializer` instead of `ModelSerializer`, you need to explicitly define `extra_kwargs` to direct the URL to the appropriate view for your polymorphic serializer. This ensures correct URL generation for specific child types.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Set `resource_type_field_name = 'your_custom_type_field'` on your `PolymorphicSerializer` subclass.","message":"By default, the polymorphic type is identified by a field named `resource_type`. If you need to use a different field name in your API requests/responses, you must override the `resource_type_field_name` attribute.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be mindful of performance for very deep inheritance hierarchies or extremely large datasets. Consider alternative inheritance strategies if performance becomes a critical bottleneck. Ensure appropriate database indexing.","message":"Using `django-polymorphic` (and consequently `django-rest-polymorphic`) for model inheritance can have performance implications. Each query to a base polymorphic model will result in `INNER JOIN` operations to fetch fields from subclassed models.","severity":"gotcha","affected_versions":"All versions (inherent to `django-polymorphic` design)"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}