I'm not really sure if it's DRF of Django issue. It seems that input should (?) be modified for DecimalField before it ends up at .validated_data. Otherwise one can pass an int and retured object from .save() would be a incorrect.
Code:
class Product(models.Model):
title = models.CharField(max_length=100)
price = models.DecimalField(
max_digits=8,
decimal_places=2,
default=Decimal('0.00'),
validators=[MinValueValidator(Decimal(0))],)
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ('title', 'price')
# create product instance
s = ProductSerializer(data={'title': 'Test', price: 100})
s.is_valid()
obj = s.save()
obj.price would be (Decimal) 100 and not (Decimal) 100.00, however serializer.data['price'] would be correctly formatted: (Decimal) 100.00; It's confusing and forces users to do obj.refresh_from_db() if one wants to use returned object and not serializer.data in their code.
I'm not really sure if it's DRF of Django issue. It seems that input should (?) be modified for DecimalField before it ends up at
.validated_data. Otherwise one can pass anintand retured object from.save()would be a incorrect.Code:
obj.pricewould be(Decimal) 100and not(Decimal) 100.00, howeverserializer.data['price']would be correctly formatted:(Decimal) 100.00; It's confusing and forces users to doobj.refresh_from_db()if one wants to use returned object and notserializer.datain their code.