So I'm working on a website that uses Django and having trouble with the serializer. Here is the code:
class DataPointSerializer(serializers.ModelSerializer):
value = serializers.DecimalField(max_digits=30, decimal_places=15)
sensor = serializers.ChoiceField(choices=list(Sensor.objects.all()))
point = serializers.ChoiceField(choices=list(MapPoint.objects.all()))
class Meta:
model = DataPoint
fields = "__all__"
def create(self, attrs, instance=None):
return DataPoint(value=attrs['value'], sensor=attrs['sensor'], point=attrs['point'])
My DataPoint model uses value as a decimal field, sensor as a foreign key, and point as another foreign key. I'm using the choice fields to fetch the objects that have been created but from the create function, I get a TypeError saying that (Sensor object) is not JSON serializable. I assume the same is happening for point but I am unsure of what to do. Any help would be appreciated!