Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The DECIMAL type field fetch data become string

In my table:

My discount type is DECIMAL:

Enter image description here

My data in table:

Enter image description here

But why when I fetch data in API, there gets string?

enter image description here

I use Django and Django REST framework as the backend.


belong_product: "server"
ctime: "2018-04-11T15:41:15.744959+08:00"
desc: ""
discount: "0.005"
id: 1
is_enable: false
max_count: 5
min_count: 0
name: "one"
uptime: "2018-04-11T15:41:15.745226+08:00"

My ListAPI view:

class DiscountItemByUserOwnCountListAPIView(ListAPIView):
    serializer_class = DiscountItemByUserOwnCountSerializer
    permission_classes = [IsSuperAdmin]
    pagination_class = CommonPagination
    def get_queryset(self):
        return DiscountItemByUserOwnCount.objects.all()

My model:

class DiscountItemByUserOwnCount(models.Model):
    name = models.CharField(max_length=16, help_text="name")
    desc = models.CharField(max_length=512, null=True, blank=True, help_text="desc")
    min_count = models.IntegerField(help_text="min")
    max_count = models.IntegerField(help_text="max")   
    discount = models.DecimalField(max_digits=4, decimal_places=3, default=0.000, unique=True,
                                   help_text="point")  
    belong_product = models.CharField(max_length=16, help_text="belongDISCOUNT_PRODUCT_TYPE")

    is_enable = models.BooleanField(default=False)

    ctime = models.DateTimeField(auto_now_add=True)
    uptime = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name
    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['min_count', '-id']
like image 355
user7693832 Avatar asked Dec 10 '25 13:12

user7693832


1 Answers

The default decimal representation in Django REST framework is string. To disable this behavior, add following to your settings.py file:

REST_FRAMEWORK = {
    'COERCE_DECIMAL_TO_STRING': False,
    # Your other settings
    ...
}

See details in the documentation.

like image 149
neverwalkaloner Avatar answered Dec 12 '25 23:12

neverwalkaloner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!