@@ -7,13 +7,17 @@ An extended JSON field for Django and Django REST framework with validation supp
7
7
8
8
## Usage
9
9
10
- Install the package:
10
+ ### Installation
11
+
12
+ To install the package, run:
11
13
12
14
``` text
13
15
pip install django_custom_jsonfield
14
16
```
15
17
16
- Import ` CustomJSONField ` and define your ` schema ` , for example:
18
+ ### Defining a model field
19
+
20
+ Import CustomJSONField and define your schema. Here’s an example of how to use it in a model:
17
21
18
22
``` python
19
23
from django.db import models
@@ -37,8 +41,8 @@ Location(coordinates={"x": 45, "z": 45}) # ValidationError
37
41
38
42
```
39
43
40
- ## DRF Serializer
41
- Just like you use ` CustomJSONField ` in your models, you can use them in serializers:
44
+ ### DRF Serializers
45
+ You can also use ` CustomJSONField ` in Django REST Framework serializers:
42
46
43
47
``` python
44
48
from rest_framework import serializers
@@ -49,4 +53,18 @@ class LocationSerializer(serializers.Serializer):
49
53
50
54
```
51
55
52
- This package automatically comes with openapi extension for ` drf-spectacular ` .
56
+ ### OpenAPI Integration
57
+ This package includes extension for ` drf-spectacular ` , allowing your API documentation
58
+ to correctly display the expected JSON schema.
59
+
60
+ ## Migrating existing data
61
+ The ` CustomJSONField ` does not impose constraints on existing data.
62
+ Therefore, you can change a field from default ` JSONField ` to ` CustomJSONField ` even if
63
+ some rows violate the schema. However, it is recommended to follow these steps to
64
+ sure a smooth transition:
65
+
66
+ 1 . ** Create a new field** : add a new field of type ` CustomJSONField ` to your model.
67
+ 2 . ** 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.
68
+ 3 . ** Replace the old field** : Remove the old field and rename the new field to match the old field's name.
69
+
70
+ Following these steps will ensure that your data complies with the new schema.
0 commit comments