Invalid data type assigned into model while update · Issue #4318 · encode/django-rest-framework (original) (raw)

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.