Jack Duffy
Jack Duffy

Reputation: 351

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.

Upvotes: 1

Views: 1972

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476503

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.

Upvotes: 1

Related Questions