serializer.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. from rest_framework import serializers
  2. from django.db.models import Max
  3. from .models import Bar,Navigation,Company,Slideshow,EditorContens,Hmwe,Hmart,\
  4. Hmcase,Hmgroup,Hmaddress,Column,WnComponentModel,WnComponentCssModel,WnComponentKeyModel,WnComponentCssGroupModel
  5. from PIL import Image
  6. # 富文本编辑器
  7. class WebEditorContensModelSerializer(serializers.ModelSerializer):
  8. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  9. class Meta:
  10. # 1,参考BookInfo,生成序列化器的字段
  11. model = EditorContens
  12. fields = '__all__'
  13. class WebBarModelSerializer(serializers.ModelSerializer):
  14. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  15. class Meta:
  16. # 1,参考BookInfo,生成序列化器的字段
  17. model = Bar
  18. fields = '__all__'
  19. class WebNavigationModelSerializer(serializers.ModelSerializer):
  20. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  21. class Meta:
  22. # 1,参考BookInfo,生成序列化器的字段
  23. model = Navigation
  24. fields = '__all__'
  25. class WebCompanyModelSerializer(serializers.ModelSerializer):
  26. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  27. class Meta:
  28. # 1,参考BookInfo,生成序列化器的字段
  29. model = Company
  30. fields = '__all__'
  31. class WebSlideshowModelSerializer(serializers.ModelSerializer):
  32. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  33. class Meta:
  34. # 1,参考BookInfo,生成序列化器的字段
  35. model = Slideshow
  36. fields = '__all__'
  37. # def list(self, validated_data):
  38. # '''重写create方法实现,将密码加密后保存'''
  39. # print(validated_data)
  40. # # return Slideshow.objects.create(**validated_data)
  41. class WebHmweModelSerializer(serializers.ModelSerializer):
  42. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  43. class Meta:
  44. # 1,参考BookInfo,生成序列化器的字段
  45. model = Hmwe
  46. fields = '__all__'
  47. class WebHmartModelSerializer(serializers.ModelSerializer):
  48. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  49. class Meta:
  50. # 1,参考BookInfo,生成序列化器的字段
  51. model = Hmart
  52. fields = '__all__'
  53. class WebHmcaseModelSerializer(serializers.ModelSerializer):
  54. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  55. class Meta:
  56. # 1,参考BookInfo,生成序列化器的字段
  57. model = Hmcase
  58. fields = '__all__'
  59. class WebColumnModelSerializer(serializers.ModelSerializer):
  60. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  61. class Meta:
  62. # 1,参考BookInfo,生成序列化器的字段
  63. model = Column
  64. fields = '__all__'
  65. class WebHmgroupModelSerializer(serializers.ModelSerializer):
  66. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  67. class Meta:
  68. # 1,参考BookInfo,生成序列化器的字段
  69. model = Hmgroup
  70. fields = '__all__'
  71. class WebHmaddressModelSerializer(serializers.ModelSerializer):
  72. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  73. class Meta:
  74. # 1,参考BookInfo,生成序列化器的字段
  75. model = Hmaddress
  76. fields = '__all__'
  77. class WnComponentSerializer(serializers.ModelSerializer):
  78. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  79. class Meta:
  80. # 1,参考BookInfo,生成序列化器的字段
  81. model = WnComponentModel
  82. fields = '__all__'
  83. class WnComponentCssSerializer(serializers.ModelSerializer):
  84. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  85. class Meta:
  86. # 1,参考BookInfo,生成序列化器的字段
  87. model = WnComponentCssModel
  88. fields = '__all__'
  89. class WnComponentKeyModelSerializer(serializers.ModelSerializer):
  90. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  91. class Meta:
  92. # 1,参考BookInfo,生成序列化器的字段
  93. model = WnComponentKeyModel
  94. fields = '__all__'
  95. def create(self, validated_data):
  96. # 查看最大排序值
  97. maxsort = WnComponentKeyModel.objects.aggregate(Max('sort'))['sort__max']
  98. print(maxsort)
  99. maxsort = maxsort if maxsort else 0
  100. maxsort = maxsort + 1
  101. validated_data['sort'] = maxsort
  102. return WnComponentKeyModel.objects.create(**validated_data)
  103. class WnComponentCssGroupSerializer(serializers.ModelSerializer):
  104. # haha = serializers.CharField(max_length=10,min_length=1,label="测试字段")
  105. class Meta:
  106. # 1,参考BookInfo,生成序列化器的字段
  107. model = WnComponentCssGroupModel
  108. fields = '__all__'