2

How to build a serializer that converts the PosixPath property into a string? I tried:

class ObjSerializer(serializers.ModelSerializer):
  class Meta():
    model = MyObject
    fields = ('slug', 'name', 'path')
    name = serializers.SerializerMethodField()
    path = serializers.SerializerMethodField()

    def get_name(self, instance):
      return instance.name

    def get_name(self, instance):
      return str( instance.path ) 

Using Django 3.

As the path is stored as PosixPath I run into:

TypeError: Object of type PosixPath is not JSON serializable

Soerendip
  • 7,684
  • 15
  • 61
  • 128

0 Answers0