Home > Software design >  Django Serialize a JsonField, Django Rest API
Django Serialize a JsonField, Django Rest API

Time:04-22

I have a class for example

from django.forms import JSONField

class Area(models.model):
  GeoJson = JSONField
  ...

and a serializer for the class

class AreaSerializer(serializers.ModelSerializer):
  model = Areas
  fields = ('GeoJson',
              ...    )

but when I try to get the data from my rest frame work I get this error

Object of type 'JSONField' is not JSON serializable

What am I doing wrong? it is my first time using django and I need to store GeoJson in the database so im really stuck on how to fix this issue as I dont understand why it wont work, Any help or advice is greatly appreciated.

CodePudding user response:

You use a form field in your model, not Django's JSONField model field [Django-doc]. It should be:

#         models            
  • Related