Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid data type assigned into model while update #4318

Closed
pySilver opened this issue Jul 28, 2016 · 2 comments
Closed

Invalid data type assigned into model while update #4318

pySilver opened this issue Jul 28, 2016 · 2 comments

Comments

@pySilver
Copy link

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.

@tomchristie
Copy link
Member

Ok, thanks for raising this

@tomchristie tomchristie added this to the 3.4.2 Release milestone Aug 1, 2016
@tomchristie
Copy link
Member

Milestoning to review further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants