serializers.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # @Author : Rocky
  2. # @File : serializers.py
  3. # @Time : 2023/7/24 13:35
  4. from rest_framework.serializers import ModelSerializer, SerializerMethodField
  5. from background.models import ProductInfo, VideoInfo, QuickStartInfo, UpgradeFirmwareInfo
  6. class ReadWriteSerializerMethodField(SerializerMethodField):
  7. """
  8. 支持可读写的SerializerMethodField
  9. 可实现Model字段和Serializer字段更加灵活地解绑
  10. 通过实现get_xxxfield方法,实现从Model的某个字段读值映射到Serializer对应字段
  11. 通过实现set_xxxfield方法,实现从Serializer字段回填值到Model对应字段
  12. """
  13. def __init__(self, method_name=None, write_method_name=None, **kwargs):
  14. self.method_name = method_name
  15. self.write_method_name = write_method_name
  16. kwargs["source"] = "*"
  17. super(SerializerMethodField, self).__init__(**kwargs)
  18. def bind(self, field_name, parent):
  19. # 绑定读函数 get_{field_name} 和写函数 set_{field_name}
  20. default_method_name = f"get_{field_name}"
  21. default_write_method_name = f"set_{field_name}"
  22. if self.method_name is None:
  23. self.method_name = default_method_name
  24. if self.write_method_name is None:
  25. self.write_method_name = default_write_method_name
  26. super(SerializerMethodField, self).bind(field_name, parent)
  27. def to_representation(self, value):
  28. # 读取过程hook
  29. method = getattr(self.parent, self.method_name)
  30. return method(value)
  31. def to_internal_value(self, data):
  32. # 写入过程hook
  33. method = getattr(self.parent, self.write_method_name)
  34. return method(data)
  35. class ProductInfoSerializer(ModelSerializer):
  36. class Meta:
  37. model = ProductInfo
  38. fields = '__all__'
  39. class VideoInSerializer(ModelSerializer):
  40. class Meta:
  41. model = VideoInfo
  42. fields = '__all__'
  43. class QuickStartInfoSerializer(ModelSerializer):
  44. class Meta:
  45. model = QuickStartInfo
  46. fields = '__all__'
  47. def create(self, validated_data):
  48. link = validated_data.get('link')
  49. validated_data['link'] = 'https://ansjerfilemanager.s3.amazonaws.com/vsees/quick_start_file/{}'.format(link)
  50. video_qs = VideoInfo.objects.create(**validated_data)
  51. return video_qs
  52. def update(self, instance, validated_data):
  53. link = 'https://ansjerfilemanager.s3.amazonaws.com/vsees/quick_start_file/{}'.format(validated_data.get('link'))
  54. instance.link = link
  55. instance.save()
  56. return instance
  57. class UpgradeFirmwareInfoSerializer(ModelSerializer):
  58. class Meta:
  59. model = UpgradeFirmwareInfo
  60. fields = '__all__'
  61. def create(self, validated_data):
  62. link = validated_data.get('link')
  63. validated_data['link'] = 'https://ansjerfilemanager.s3.amazonaws.com/vsees/upgrade_firmware_file/{}'.format(
  64. link)
  65. video_qs = VideoInfo.objects.create(**validated_data)
  66. return video_qs
  67. def update(self, instance, validated_data):
  68. link = 'https://ansjerfilemanager.s3.amazonaws.com/vsees/upgrade_firmware_file/{}'.format(
  69. validated_data.get('link'))
  70. instance.link = link
  71. instance.save()
  72. return instance