Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading