Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
Allow using class properties of models for populating fields in serializers#9623
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
One of the features I miss in DRF is mapping serializer fields to properties of selected models. (It could be a good alternative to someReadWriteSerializerMethodFields ) Desired usage example:
# We have a model which stores some data in a different format than we are going to serve itclassLocation(models.Model):lattitude_degrees=models.FloatField()@propertydeflat_radians(self):returndeg_to_rad(self.lattitude_degrees)@lat_radians.setterdeflat_radians(self,lat:float):self.lattitude_degrees=rad_to_deg(lat)
classLocationSerializer(serializers.ModelSerializer):classMeta:model=models.Locationfields= ["lat_radians"]lat_radians=serializers.FloatField(source="lat_radians") |
BetaWas this translation helpful?Give feedback.
All reactions
Ithink it's becauseModelSerializer
cannot infer the field type without a model field.
Have you tried the following?
classLocationSerializer(serializers.ModelSerializer):lat_radians=serializers.FloatField()classMeta:model=models.Locationfields= ["lat_radians"]
Replies: 1 comment 2 replies
-
Ithink it's because Have you tried the following? classLocationSerializer(serializers.ModelSerializer):lat_radians=serializers.FloatField()classMeta:model=models.Locationfields= ["lat_radians"] |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I updated my example to address this issue.
I will check this out and let you know |
BetaWas this translation helpful?Give feedback.
All reactions
-
It works as you said. Thank you for quick answer |
BetaWas this translation helpful?Give feedback.