oxan / djangorestframework-dataclasses

Dataclasses serializer for Django REST framework
BSD 3-Clause "New" or "Revised" License
431 stars 28 forks source link

Enable DataclassSerializer to work with property #44

Closed EnriqueSoria closed 3 years ago

EnriqueSoria commented 3 years ago

It looks like DataclassSerializer can't handle properties automatically. It would be great to make this possible:

@dataclass
class PricingInfo:
    original_price_incl_tax: Decimal

    @property
    def original_price(self) -> Decimal:
        """Backwards compatibility"""
        return self.original_price_incl_tax
class PricingInfoSerializer(DataclassSerializer):
    class Meta:
        dataclass = PricingInfo
        fields = (
            "original_price",
        )
The field 'original_price' was included on serializer PricingInfoSerializer in the `fields` option, but does not match any dataclass field.
EnriqueSoria commented 3 years ago

Thanks!