Skip to content

Commit

Permalink
Change return value of get_schema_type from string to type
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertKolner committed Nov 5, 2024
1 parent d61837d commit 1f08938
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
9 changes: 4 additions & 5 deletions docs/docs/guides/response/django-pydantic.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,13 @@ class TranslatedTextFieldSchema(Schema):
fr: Optional[str] = None
```

To achieve that, we can add a `get_schema_type` function to the django field and add it to supported
types in Django Ninja:
To achieve that, we can add a `get_schema_type` function to the django field and make it return
the desired type:

```python
class TranslatedTextField(models.JSONField):
...
def get_schema_type(self):
return "TranslatedTextField"

ninja.orm.fields.TYPES["TranslatedTextField"] = TranslatedTextFieldSchema
return TranslatedTextFieldSchema

```
10 changes: 5 additions & 5 deletions ninja/orm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ def get_schema_field(
max_length = field_options.get("max_length")

if hasattr(field, 'get_schema_type'):
internal_type = field.get_schema_type()
python_type = field.get_schema_type()
else:
internal_type = field.get_internal_type()

try:
python_type = TYPES[internal_type]
except KeyError as e:
raise KeyError("Type '{0}' isn't registered in ninja.orm.fields.TYPES".format(internal_type)) from e
try:
python_type = TYPES[internal_type]
except KeyError as e:
raise KeyError("Type '{0}' isn't registered in ninja.orm.fields.TYPES".format(internal_type)) from e

if field.primary_key or blank or null or optional:
default = None
Expand Down

0 comments on commit 1f08938

Please sign in to comment.