-from django.contrib.contenttypes.models import ContentType
+from django.shortcuts import get_object_or_404
+from django.contrib.contenttypes.models import ContentType
from rest_framework.serializers import (
HyperlinkedModelSerializer,
HyperlinkedRelatedField,
+ Serializer,
+ CharField,
+ IntegerField,
)
from rest_framework.reverse import reverse
@@ -157,6 +161,23 @@ Source code for apis_core.generic.serializers
+
+
+
+
[docs]
+
class ContentTypeInstanceSerializer(Serializer):
+
id = IntegerField(required=True)
+
content_type = CharField(required=True)
+
+
+
[docs]
+
def to_internal_value(self, data):
+
data = super().to_internal_value(data)
+
app_label, model = data.get("content_type").split(".")
+
content_type = get_object_or_404(ContentType, app_label=app_label, model=model)
+
return get_object_or_404(content_type.model_class(), pk=data.get("id"))
+
+