Empty Dynamic Nested Serializer for Generic Django Model

I have this generic Model called Thumbs with two fields which can describe another model. The two fields are:

  • 'entity_type', a CharField
    • values can be a family or product, both corresponding to Family and Product models, respectively
  • Then there's 'entity_id', a IntegerField
    • this is the PrimaryKey for the previous entity in another table.

So, I want to create a serializer for this Thumbs model which implements a nested serializer for the specified 'entity_type'. For example, if the 'entity_type' is family, the serializer used a FamilySerializer so the serialized Thumbs object has the entity's data.

This is how I implemented that behavior, but it is returning an empty family object.

from rest_framework import serializers
from app.models import Thumbs, Family
from app.serializers.user_role_product_tool import FamilySerializer, ProductSerializer

class ThumbSerializer(serializers.ModelSerializer):
    entity = serializers.SerializerMethodField()

    class Meta:
        model = Thumbs
        fields = '__all__'

    def get_entity(self, obj):
        if obj.entity_type == "family":
            return FamilySerializer(source=obj.entity_id, read_only=True).data
        
        else:
            return None

Example output showing the Family serializer is being used when expected, but it's empty although the Family table has an entry with PrimaryKey of 107:

{
    "id": 182,
    "entity": {
      "family_name": "",
      "status": "",
      "created_by": "",
      "last_updated_by": ""
    },
    "entity_id": "107",
    "entity_type": "family",
    "thumbs_decision": "PENDING",
    "entity_parent_id": "181",
    "created_date": "2024-09-03T14:21:17.403159-07:00",
    "last_updated_by": "",
    "last_updated_date": "2024-09-03T14:21:17.403217-07:00",
    "created_by": "guest"
  },
  {
    "id": 212,
    "entity": null,
    "entity_id": "951",
    "entity_type": "product",
    "thumbs_decision": "PENDING",
    "entity_parent_id": "181",
    "created_date": "2024-09-03T14:21:18.647675-07:00",
    "last_updated_by": "",
    "last_updated_date": "2024-09-03T14:21:18.647712-07:00",
    "created_by": "guest"
  },

A Family model object with the same id value as obj.entity_id was obtained and returned using serializer.

from rest_framework import serializers
from app.models import Thumbs, Family
from app.serializers.user_role_product_tool import FamilySerializer, ProductSerializer

class FamilySerializer(serializers.ModelSerializer):
    class Meta:
        model = Family
        fields = '__all__'

class ThumbSerializer(serializers.ModelSerializer):
    entity = serializers.SerializerMethodField()

    class Meta:
        model = Thumbs
        fields = '__all__'

    def get_entity(self, obj):
        if obj.entity_type == "family":
            family = Family.objects.get(id=obj.entity_id)
            serializer = FamilySerializer(family)
            return serializer.data
        else:
            return None

Does the code like above work for your purpose?

Back to Top