Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
1
Indexable
Never
class GamesListSerializer(serializers.ModelSerializer):
    price = serializers.SerializerMethodField()
    # TODO реализовать систему скидок
    discount = serializers.IntegerField(default=0)
    # TODO продумать систему оценок
    is_bought = serializers.BooleanField(default=False)
    is_favorite = serializers.BooleanField(default=False)

    system_requirements = ShortSystemReqSerializers(many=True, read_only=True)
    genres = serializers.StringRelatedField(many=True)

    def get_price(self, obj):
        try:
            offer = ProductOffer.objects.get(product=obj).offer
        except ProductOffer.DoesNotExist:
            return None
        return offer.price.amount

    class Meta:
        model = Product
        fields = (
            'id',
            'name',
            'release_date',
            'genres',
            'system_requirements',
            'price',
            'discount',
            'is_bought',
            'is_favorite'
        )