'Django Serialize a JsonField, Django Rest API

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.



Solution 1:[1]

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

#         models ?
from django.db.models import JSONField

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

You however might be interested to work with a spatial field type [Django-doc], and take a look at django-geojson [GitHub] for GeoJSON functionality.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1