I'm trying to serialize a model instance of type Shift
but I'm getting an error.
'Shift' object has no attribute 'get'
shift = models.Shift.objects.get(pk=8)
ser = serializers.ShiftSerializer(many=False, data=shift)
ser.is_valid()
ShiftSerializer is a ModelSerializer.
This works if I get the shift using filter
and all
and many=True
.
Solution:
shift = models.Shift.objects.get(pk=8)
ser = serializers.ShiftSerializer(shift)
The HyperlinkedModelSerializer class is similar to the ModelSerializer class except that it uses hyperlinks to represent relationships, rather than primary keys. By default the serializer will include a url field instead of a primary key field.
The data
parameter is for deserializing, not serializing. You should just pass the model instance as a positional arg.
obj = serializers.ShiftSerializer(shift)
Note there's no need to specify many=False, that's the default. Also, it doesn't make sense to call is_valid()
on the serializer you've constructed from a model instance; again, that's for deserialization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With