123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- # coding=utf-8
- from django.conf import settings
- from rest_framework import serializers
- from apps.product.models import Product, ProductImg
- from apps.base import Formater
- from apps.foundation.models import Option
- class ProductSerializer(serializers.ModelSerializer):
- cover_url = serializers.SerializerMethodField()
- price_f = serializers.SerializerMethodField()
- def get_cover_url(self, obj):
- if obj.cover:
- return {'width': obj.cover.width, 'height': obj.cover.height, 'url': obj.cover.get_path()}
- return ''
- def get_price_f(self, obj):
- return Formater.formatPriceShow(obj.price)
- class Meta:
- model = Product
- fields = '__all__'
- class ProductDetailSerializer(serializers.ModelSerializer):
- price_f = serializers.SerializerMethodField()
- Product_images = serializers.SerializerMethodField()
- has_cover = serializers.SerializerMethodField()
- cover = serializers.SerializerMethodField()
- def get_cover(self, obj):
- if obj.cover:
- return {'width': obj.cover.width, 'height': obj.cover.height, 'url': obj.cover.get_path()}
- return ''
- def get_has_cover(self, obj):
- if obj.cover:
- return True
- return False
- def get_Product_images(self, obj):
- result = []
- rows = ProductImg.objects.filter(product=obj).values('img__picture', 'img__width', 'img__height')
- for row in rows:
- url = {'width': row['img__width'], 'height': row['img__height'], 'url': '%s%s%s' % (settings.SERVER_DOMAIN, settings.MEDIA_URL, row['img__picture'])}
- result.append(url)
- return result
- def get_price_f(self, obj):
- return Formater.formatAmountShow(obj.price)
- class Meta:
- model = Product
- fields = '__all__'
- class ProductTypeSerializer(serializers.ModelSerializer):
- class Meta:
- model = Option
- fields = ('id', 'name')
|