Skip to content

Invalid data type assigned into model while update #4318

@pySilver

Description

@pySilver

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy