Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for postgres ArrayField #386

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Install core
run: pip install "Django${{ matrix.django-version }}" pydantic
- name: Install tests
run: pip install pytest pytest-asyncio pytest-django
run: pip install pytest pytest-asyncio pytest-django psycopg2
- name: Test
run: pytest

Expand Down
18 changes: 16 additions & 2 deletions ninja/orm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@

from django.db.models import ManyToManyField
from django.db.models.fields import Field

try:
from django.contrib.postgres.fields.array import ArrayField
except ModuleNotFoundError: # pragma no cover
# psycopg2 not installed. ok to make a dummy here since it can't be in use
class ArrayField: # type: ignore[no-redef]
pass


from pydantic import IPvAnyAddress
from pydantic.fields import FieldInfo, Undefined

Expand Down Expand Up @@ -117,8 +126,13 @@ def get_schema_field(field: Field, *, depth: int = 0) -> Tuple:
null = field_options.get("null", False)
max_length = field_options.get("max_length")

internal_type = field.get_internal_type()
python_type = TYPES[internal_type]
if isinstance(field, ArrayField):
inner_internal_type = field.base_field.get_internal_type()
inner_python_type = TYPES[inner_internal_type]
python_type = List[inner_python_type] # type: ignore[valid-type]
else:
internal_type = field.get_internal_type()
python_type = TYPES[internal_type]

if field.has_default():
if callable(field.default):
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ test = [
"flake8",
"mypy==0.931",
"django-stubs",
"psycopg2",
]
doc = [
"mkdocs",
Expand Down
8 changes: 8 additions & 0 deletions tests/test_orm_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import django
import pytest
from django.contrib.postgres.fields.array import ArrayField
from django.db import models
from django.db.models import Manager

Expand Down Expand Up @@ -45,6 +46,7 @@ def test_all_fields():
# test all except relational field

class AllFields(models.Model):
arrayfield = ArrayField(models.IntegerField())
bigintegerfield = models.BigIntegerField()
binaryfield = models.BinaryField()
booleanfield = models.BooleanField()
Expand Down Expand Up @@ -82,6 +84,11 @@ class Meta:
"type": "object",
"properties": {
"id": {"title": "Id", "type": "integer"},
"arrayfield": {
"title": "Arrayfield",
"type": "array",
"items": {"type": "integer"},
},
"bigintegerfield": {"title": "Bigintegerfield", "type": "integer"},
"binaryfield": {
"title": "Binaryfield",
Expand Down Expand Up @@ -139,6 +146,7 @@ class Meta:
"uuidfield": {"title": "Uuidfield", "type": "string", "format": "uuid"},
},
"required": [
"arrayfield",
"bigintegerfield",
"binaryfield",
"booleanfield",
Expand Down