Using Django JSONField in model

Issue

I am creating REST API’s.

django==3.2.2
djangorestframework==3.12.4
psycopg2==2.8.6

I am new to Django, python.
I looking for a way to use the JSON field in the Django model.
My model looks like below –

class Question(BaseModel):
.... other code...
    attributes = models.JSONField()

Now I want the attributes to be a JSON, like below

{
    "index": 0,
    "guid": "95161b18-75a8-46bf-bb1f-6d1e16e3d60b",
    "isActive": false,
    "latitude": -25.191983,
    "longitude": -123.930584,
    "tags": [
      "esse",
      "sunt",
      "quis"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Contreras Weeks"
      },
      {
        "id": 1,
        "name": "Dawn Lott"
      }
    ]
}

Should I create a new model, but creating a new model will make it add to migrations which I do not want.

  1. How to create a model for the above?
  2. How can I use the default validation provided by Django ORM on it?

Solution

I figured out – we can use pydantic or schema for django. They offers validations also. I preferred pydantic.

  1. https://github.com/keleshev/schema
  2. https://pydantic-docs.helpmanual.io/

EDIT

Pydentic example

Schema

from typing import List
from pydantic import (
    BaseModel,
    StrictBool,
    StrictInt,
    StrictStr,
)

class Foo(BaseModel):
count: int
size: float = None


class Bar(BaseModel):
    apple = 'x'
    banana = 'y'

class AttributesSchema(BaseModel):
    point: StrictInt
    value: StrictStr
    foo: Foo
    bars: List[Bar]

will return JSON like

{
    'point': 2,
    'value': 'Any string'
    'foo': {'count': 4, 'size': None},
    'bars': [
        {'apple': 'x1', 'banana': 'y'},
        {'apple': 'x2', 'banana': 'y'},
    ],
}

Validation –
Add this to your serializer

 schema = AttributesSchema
 try:
   errors = schema.validate(data['attributes'])
 except Exception as errors:
   raise serializers.ValidationError(errors)

Refer pydantic documents, it has everything we need.
https://pydantic-docs.helpmanual.io/usage/types/

Answered By – Ishika Jain

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published