If you want to serialize a shared foreign key, you need to define a custom field to clearly define how you want to serialize the goals of the relationship.
If your AdditionalInfo model has a common relationship with the SealType1 and SealType2 , you can see an example below.
class SealRelatedField(serializers.RelatedField): def to_native(self, value): """ Serialize seal object to whatever you need. """ if isinstance(value, SealType1): return ... elif isinstance(value, SealType2): return ... raise Exception('Unexpected type of tagged object')
More details on the Django REST infrastructure can be found.
source share