Closed amandeep-r closed 5 years ago
Can you provide an example of a problematic serializer/view?
Sure,
So here is the relevant part of my model:
class Pack(models.Model):
SIZE_10x20 = Decimal(200.000)
SIZE_10x10 = Decimal(100.000)
SIZE_5x10 = Decimal(50.000)
size_code_choices = (
(SIZE_5x10, '5x10'),
(SIZE_10x10, '10x10'),
(SIZE_10x20, '10x20'),
)
size_code = models.DecimalField(max_digits=7,
decimal_places=3,
choices=size_code_choices,
default=SIZE_10x20)
The serializer:
class HarvestSerializer(serializers.ModelSerializer):
harvest_signal = serializers.BooleanField(required=True, write_only=True)
class Meta:
model = Pack
fields = (
'harvest_signal',
'pack_uuid',
'size_code'
)
read_only_fields = (
'pack_uuid',
'size_code'
)
And the viewset:
class HarvestViewSet(mixins.ListModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):
queryset = Pack.objects.all()
serializer_class = HarvestSerializer
permission_classes = [permissions.IsAuthenticated, api_permissions.HarvestPermissions]
authentication_classes = [TokenAuthentication]
def perform_update(self, serializer):
instance = serializer.instance
logger.debug("Performing Update!")
if serializer.validated_data['harvest_signal'] is True:
#Business Logic Here ---
else:
pass
serializer.save()
Appreciate the speedy reply!
I have other models and serializers that use DecimalFields. I highly suspect it has something to do with the choices constraint on this particular model field.
Hello,
I'm getting the following Type Error after I added a decimal field to my model:
Decimal('50') is not JSON serializable
I'm using Django version 2.1.7 and drf_yasg version 1.14.0. I looked into setting COERCE_DECIMAL_TO_STRING in my DRF setting to true but no luck. There was a similar issue (#62 ) earlier but I was unable to use that information to help with my current issue.
Here is my traceback: