Modelserializer in django rest_framework serializes MeasurementField as FloatField
See original GitHub issuehi,
I have the following field in my model:
MeasurementField(measurement=Weight, unit_choices=(('kg','kg'),('lb','lb'),))
which serializes as a FloatField() in django rest framework using serializers.ModelSerializer. This FloatField() then does not accept a Mass() object so I land on a TypeError.
Anyone with an idea how to rewrite the serializers to_represantation and to_internal_value to handle this error? I tried this which works for the representation part:
def to_representation(self, obj):
return {
'weight': obj.weight.value,
'unit': obj.weight.unit}
But I cannot get the to_internal_value to work. Besides, there are other fields in the model which I have to rewrite if I use this approach. Thanks in advance for any suggestion!
Could it be an enhancement to include a serializerField in the MeasurementField definition?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6
Top Results From Across the Web
Serializer fields - Django REST framework
Serializer fields handle converting between primitive values and internal datatypes. They also deal with validating input values, ...
Read more >Numeric fields in serializers - Django REST Framework
This article revolves around Numeric Fields in Serializers in Django REST Framework. There are three major fields – IntegerField, FloatField ...
Read more >How to serialize creation with Django Rest Framework ...
I can't comment due to low rep but here's the function from the classyDRF def create(self, request, *args, **kwargs): serializer ...
Read more >Handling Model Relationships in Django Rest Framework
Make a note of the related_field = “ingredients" attribute. This is required for the serializer for Product to get access to the related ......
Read more >Creating a REST API with Django REST Framework
The serializer accepts the JSON data, deserializes it into a concrete object and then serializes it again, to return a response. You can...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I looked into this further and I think the problem is that there is no MultiValueField among the serializers.Fields in DRF, like there is in forms.Fields, so DRF cannot handle what is in your forms.py line 99-110. Maybe this is a problem more for DRF then for your package. At least, I couldn’t find anything that would correspond to forms.MultiValueField in DRF. I ended up defining a custom field like so:
I still need to add validation that no other units are entered in the form field, but it works. Only found this solution yesterday.
how about something like this?