{"id":15012962,"url":"https://github.com/alexpetul/django_custom_jsonfield","last_synced_at":"2026-01-04T22:08:36.981Z","repository":{"id":240677422,"uuid":"799180028","full_name":"AlexPetul/django_custom_jsonfield","owner":"AlexPetul","description":"An extended JSON field for Django and DRF with validation support using jsonschema.","archived":false,"fork":false,"pushed_at":"2024-05-22T19:44:36.000Z","size":31,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-05-22T19:47:30.823Z","etag":null,"topics":["django","drf","drf-spectacular","jsonschema","rest-framework"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlexPetul.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-11T11:44:04.000Z","updated_at":"2024-05-22T19:01:23.000Z","dependencies_parsed_at":"2024-05-22T19:56:50.541Z","dependency_job_id":null,"html_url":"https://github.com/AlexPetul/django_custom_jsonfield","commit_stats":null,"previous_names":["alexpetul/django_custom_jsonfield"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexPetul%2Fdjango_custom_jsonfield","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexPetul%2Fdjango_custom_jsonfield/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexPetul%2Fdjango_custom_jsonfield/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexPetul%2Fdjango_custom_jsonfield/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexPetul","download_url":"https://codeload.github.com/AlexPetul/django_custom_jsonfield/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244583066,"owners_count":20476233,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","drf","drf-spectacular","jsonschema","rest-framework"],"created_at":"2024-09-24T19:43:31.725Z","updated_at":"2026-01-04T22:08:36.863Z","avatar_url":"https://github.com/AlexPetul.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# django_custom_jsonfield\n\n![Test](https://github.com/alexpetul/django_custom_jsonfield/actions/workflows/test.yml/badge.svg)\n[![Coverage](https://codecov.io/github/AlexPetul/django_custom_jsonfield/graph/badge.svg?token=V33XNC6SZ7)](https://codecov.io/github/AlexPetul/django_custom_jsonfield)\n\nAn extended JSON field for Django and Django REST framework with validation support using [jsonschema](https://json-schema.org/learn/getting-started-step-by-step).\n\n## Usage\n\n### Installation\n\nTo install the minimal version of the package, run:\n\n```text\npip install django_custom_jsonfield\n```\n\n### Defining a model field\n\nImport CustomJSONField and define your schema. Here’s an example of how to use it in a model:\n\n```python\nfrom django.db import models\nfrom django_custom_jsonfield import CustomJSONField\n\n\nclass Location(models.Model):\n    coordinates = CustomJSONField(\n        schema={\n            \"type\": \"object\",\n            \"properties\": {\n                \"x\": {\"type\": \"number\"}, \n                \"y\": {\"type\": \"number\"},\n            },\n            \"required\": [\"x\", \"y\"],\n        },\n    )\n\nLocation(coordinates={\"x\": 45, \"y\": 45})  # ok\nLocation(coordinates={\"x\": 45, \"z\": 45})  # ValidationError\n```\n\nYou can customize the error message, if the value didn't pass JSON schema validation:\n\n```python\nclass Location(models.Model):\n    coordinates = CustomJSONField(\n        schema={...},\n        error_messages={\"invalid_data\": \"Expected x and y keys.\"},\n    )\n```\n\n### DRF Serializers\nTo enable DRF support, install package with extras:\n\n```text\npip install 'django_custom_jsonfield[drf]'\n```\n\nYou can now use `CustomJSONField` in DRF serializers:\n\n```python\nfrom rest_framework import serializers\nfrom django_custom_jsonfield.rest_framework.serializers import CustomJSONField\n\nclass LocationSerializer(serializers.Serializer):\n    address = CustomJSONField(schema={\"type\": \"string\"})\n```\n\nTo specify custom error message use the same positional argument as you use in models:\n```python\nclass LocationSerializer(serializers.Serializer):\n    address = CustomJSONField(\n        schema={\"type\": \"string\"}, \n        error_messages={\"invalid_data\": \"Expected type `string`.\"},\n    )\n```\n\n### OpenAPI Integration\nThis package includes extension for `drf-spectacular`, allowing your API documentation \nto correctly display the expected JSON schema. To access this feature, install the package with the `[drf]` extra.\n\n## Migrating existing data\nThe `CustomJSONField` does not impose any constraints on existing data. \nTherefore, you can change a field from default `JSONField` to `CustomJSONField` even if \nsome rows violate the schema. However, it is recommended to follow these steps to \nensure a smooth transition:\n\n1. **Create a new field**: add a new field of type `CustomJSONField` to your model.\n2. **Data migration**: Perform a data migration to copy the values from the old field to the new field, ensuring the data conforms to the schema.\n3. **Replace the old field**: Remove the old field and rename the new field to match the old field's name.\n\nFollowing these steps will ensure that your data complies with the new schema.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexpetul%2Fdjango_custom_jsonfield","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexpetul%2Fdjango_custom_jsonfield","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexpetul%2Fdjango_custom_jsonfield/lists"}