serializers.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # coding=utf-8
  2. from django.conf import settings
  3. from rest_framework import serializers
  4. from utils.exceptions import CustomError
  5. from apps.base import Formater
  6. from apps.images.models import Images
  7. from .models import ProductOrder
  8. class ProductOrderSerializer(serializers.ModelSerializer):
  9. product_name = serializers.CharField(source='product.name', read_only=True)
  10. name = serializers.CharField(source='customer.name', read_only=True)
  11. customer_name = serializers.CharField(source='customer_address.name', read_only=True)
  12. customer_tel = serializers.CharField(source='customer_address.tel', read_only=True)
  13. area = serializers.CharField(source='customer_address.area', read_only=True)
  14. addr = serializers.CharField(source='customer_address.addr', read_only=True)
  15. status_text = serializers.CharField(source='get_status_display', read_only=True)
  16. create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M', read_only=True)
  17. price = serializers.SerializerMethodField()
  18. amount = serializers.SerializerMethodField()
  19. def get_price(self, obj):
  20. return Formater.formatPriceShow(obj.price)
  21. def get_amount(self, obj):
  22. return Formater.formatAmountShow(obj.amount)
  23. class Meta:
  24. model = ProductOrder
  25. fields = '__all__'
  26. def validate(self, attrs):
  27. if 'price' in attrs:
  28. attrs['price'] = Formater.formatPrice(attrs['price'])
  29. return attrs