Skip to content
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: 2 additions & 0 deletions ninja/orm/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def create_schema(
fields: Optional[List[str]] = None,
exclude: Optional[List[str]] = None,
optional_fields: Optional[List[str]] = None,
blank_nullable: bool = True,
custom_fields: Optional[List[Tuple[str, Any, Any]]] = None,
base_class: Type[Schema] = Schema,
) -> Type[Schema]:
Expand All @@ -66,6 +67,7 @@ def create_schema(
fld,
depth=depth,
optional=optional_fields and (fld.name in optional_fields),
blank_nullable=blank_nullable,
)
definitions[fld.name] = (python_type, field_info)

Expand Down
8 changes: 6 additions & 2 deletions ninja/orm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ def _validate(cls, v: Any, _):

@no_type_check
def get_schema_field(
field: DjangoField, *, depth: int = 0, optional: bool = False
field: DjangoField,
*,
depth: int = 0,
optional: bool = False,
blank_nullable: bool = True,
) -> Tuple:
"Returns pydantic field from django's model field"
alias = None
Expand Down Expand Up @@ -163,7 +167,7 @@ def get_schema_field(
]
raise ConfigError("\n".join(msg)) from e

if field.primary_key or blank or null or optional:
if field.primary_key or (blank_nullable and blank) or null or optional:
default = None
nullable = True

Expand Down
5 changes: 5 additions & 0 deletions ninja/orm/metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class MetaConf:
fields: Optional[List[str]] = None
exclude: Union[List[str], str, None] = None
fields_optional: Union[List[str], str, None] = None
blank_nullable: bool = True

@staticmethod
def from_schema_class(name: str, namespace: dict) -> "MetaConf":
Expand All @@ -26,13 +27,15 @@ def from_schema_class(name: str, namespace: dict) -> "MetaConf":
fields = getattr(meta, "fields", None)
exclude = getattr(meta, "exclude", None)
optional_fields = getattr(meta, "fields_optional", None)
blank_nullable = getattr(meta, "blank_nullable", True)

elif "Config" in namespace:
config = namespace["Config"]
model = config.model
fields = getattr(config, "model_fields", None)
exclude = getattr(config, "model_exclude", None)
optional_fields = getattr(config, "model_fields_optional", None)
blank_nullable = getattr(config, "blank_nullable", True)

warnings.warn(
"The use of `Config` class is deprecated for ModelSchema, use 'Meta' instead",
Expand Down Expand Up @@ -62,6 +65,7 @@ def from_schema_class(name: str, namespace: dict) -> "MetaConf":
fields=fields,
exclude=exclude,
fields_optional=optional_fields,
blank_nullable=blank_nullable,
)


Expand Down Expand Up @@ -109,6 +113,7 @@ def __new__(
fields=meta_conf.fields,
exclude=meta_conf.exclude,
optional_fields=meta_conf.fields_optional,
blank_nullable=meta_conf.blank_nullable,
custom_fields=custom_fields,
base_class=cls,
)
Expand Down
74 changes: 74 additions & 0 deletions tests/test_orm_metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,80 @@ class Meta:
}


def test_blank_nullable():
class BlankTestModel(models.Model):
field1 = models.CharField(blank=True, null=False)
field2 = models.IntegerField(blank=False, null=False)
field3 = models.DateField(blank=True, null=True)
field4 = models.FileField(blank=False, null=True)
field5 = models.TextField(blank=False, null=False)

class Meta:
app_label = "tests"

class BlankTestSchema1(ModelSchema):
class Meta:
model = BlankTestModel
fields = "__all__"
fields_optional = ["field5"]

class BlankTestSchema2(ModelSchema):
class Meta:
model = BlankTestModel
fields = "__all__"
fields_optional = ["field5"]
blank_nullable = False

assert BlankTestSchema1.json_schema() == {
"title": "BlankTestSchema1",
"type": "object",
"properties": {
"id": {"title": "ID", "anyOf": [{"type": "integer"}, {"type": "null"}]},
"field1": {
"title": "Field1",
"anyOf": [{"type": "string"}, {"type": "null"}],
},
"field2": {"title": "Field2", "type": "integer"},
"field3": {
"title": "Field3",
"anyOf": [{"type": "string", "format": "date"}, {"type": "null"}],
},
"field4": {
"title": "Field4",
"anyOf": [{"type": "string"}, {"type": "null"}],
},
"field5": {
"title": "Field5",
"anyOf": [{"type": "string"}, {"type": "null"}],
},
},
"required": ["field2"],
}

assert BlankTestSchema2.json_schema() == {
"title": "BlankTestSchema2",
"type": "object",
"properties": {
"id": {"title": "ID", "anyOf": [{"type": "integer"}, {"type": "null"}]},
"field1": {"title": "Field1", "type": "string"},
"field2": {"title": "Field2", "type": "integer"},
"field3": {
"title": "Field3",
"anyOf": [{"type": "string", "format": "date"}, {"type": "null"}],
},
"field4": {
"title": "Field4",
"anyOf": [{"type": "string"}, {"type": "null"}],
},
"field5": {
"title": "Field5",
"anyOf": [{"type": "string"}, {"type": "null"}],
},
},
"required": ["field1", "field2"],
}


def test_model_schema_without_config():
with pytest.raises(
ConfigError,
Expand Down