Skip to content

Commit

Permalink
Fix #72: Allow providing custom schema for ArrayField
Browse files Browse the repository at this point in the history
  • Loading branch information
bhch committed Sep 13, 2024
1 parent ce232f3 commit 54926ba
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions django_jsonform/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def __init__(self, base_field, **kwargs):
self.min_items = kwargs.get('min_length')

self.nested = kwargs.pop('nested', False)
self.schema = kwargs.pop('schema', None)

if not self.nested:
self.widget = JSONFormWidget(schema=self.get_schema())
Expand All @@ -107,6 +108,9 @@ def to_python(self, value):
return super().to_python(value)

def get_schema(self):
if self.schema:
return self.schema

schema = {'type': 'array'}

if isinstance(self.base_field, ArrayFormField):
Expand Down
8 changes: 7 additions & 1 deletion django_jsonform/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ def __init__(self, *args, **kwargs):
raise ImproperlyConfigured('ArrayField requires psycopg2 to be installed.')

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.7, 2.2)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.7, 3.2)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.8, 2.2)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.8, 3.2)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.8, 4.1)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.8, 4.2)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.9, 2.2)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.9, 3.2)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.9, 4.1)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.9, 4.2)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.10, 3.2)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.10, 4.1)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.10, 4.2)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.10, main)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.11, 4.1)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.11, 4.2)

ArrayField requires psycopg2 to be installed.

Check failure on line 56 in django_jsonform/models/fields.py

View workflow job for this annotation

GitHub Actions / build (3.11, main)

ArrayField requires psycopg2 to be installed.

self.nested = kwargs.pop('nested', False)
self.schema = kwargs.pop('schema', None)
super().__init__(*args, **kwargs)

def formfield(self, **kwargs):
return super().formfield(**{'form_class': ArrayFormField, 'nested': self.nested, **kwargs})
return super().formfield(**{
'form_class': ArrayFormField,
'nested': self.nested,
'schema': self.schema,
**kwargs,
})
11 changes: 10 additions & 1 deletion tests/models/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import TestCase
from unittest.mock import MagicMock, patch
from django_jsonform.models.fields import JSONField
from django_jsonform.models.fields import JSONField, ArrayField
from django.db import models


class JSONFieldTests(TestCase):
Expand All @@ -11,3 +12,11 @@ def test_calls_pre_save_hook_if_provided(self):
model_instance = MagicMock()
field.pre_save(model_instance, True)
pre_save_hook.assert_called_once()


class ArrayFieldTests(TestCase):
def test_allows_providing_custom_schema(self):
schema = {'type': 'array', 'items': {'type': 'string'}}

# initialising the ArrayField with custom schema must succeed
field = ArrayField(models.CharField(max_length=10), schema=schema)

0 comments on commit 54926ba

Please sign in to comment.