perf(application): 优化RemoteApp应用Chrome序列类的字符串主键关联字段

pull/4941/head
Bai 2020-11-05 13:11:16 +08:00 committed by Jiangjie.Bai
parent 6df331cbed
commit eabaae81ac
1 changed files with 3 additions and 9 deletions

View File

@ -21,17 +21,11 @@ logger = get_logger(__file__)
class CharPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
def to_internal_value(self, data):
if self.pk_field is not None:
data = self.pk_field.to_internal_value(data)
try:
instance = self.get_queryset().get(pk=data)
return str(instance.id)
except ObjectDoesNotExist:
self.fail('does_not_exist', pk_value=data)
except (TypeError, ValueError):
self.fail('incorrect_type', data_type=type(data).__name__)
instance = super().to_internal_value(data)
return str(instance.id)
def to_representation(self, value):
# value is instance.id
if self.pk_field is not None:
return self.pk_field.to_representation(value)
return value