class ContentTypeField serializers CharField def to_internal_value sel

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class ContentTypeField(serializers.CharField):
def to_internal_value(self, data):
if not data:
return data
app_label, model = data.split('.')
return ContentType.objects.get_by_natural_key(app_label, model)
def to_representation(self, value):
if not value:
return value
return ".".join(value.natural_key())