DRF, Getters Setters, PolymorphicModel
✓ Published0🌍 Public
Aandybak
Last edited Apr 10, 2014
Created on Apr 10, 2014
This example shows a Django REST Framework serializer that exposes a computed `driver` property on a polymorphic model, delegating to either the delivery or collection driver based on the model’s type. The `models.py` file defines `MyModel` with a `PolymorphicModel` base, using `@property` and `@setter` to route reads and writes to the correct foreign-key field. The `serializers.py` class `MyModelSerializer` subclasses `ModelSerializer` and declares `driver = UserSerializer()` to output the nested user object. The code demonstrates how to unify two underlying fields into a single API field without custom serializer logic.
AI-generated description