Discussion on django model postgres json field encoding problem

  • 2020-06-23 01:00:55
  • OfStack

The encoder of the json field of django model cannot effectively encode data types such as uuid, datetime, etc. When the object of this type is directly stored in the json field to throw an encoding exception, then the json encoder can be specified through the encoder parameter of the JSONField field, and JSONEncoder of django rest framework is directly used here


from django.contrib.postgres.fields import JSONField
from rest_framework.utils.encoders import JSONEncoder

# django.contrib.postgres.fields.jsonb.JSONField(default=dict, encoder=rest_framework.utils.encoders.JSONEncoder ) 

detail = JSONField(default=dict, encoder=JSONEncoder, help_text=u' details ')

Related articles: