Skip to content

Commit

Permalink
Merge pull request #986 from vitalik/fields-optional
Browse files Browse the repository at this point in the history
Fields not generated when  Meta.fields_optional = '__all__'
  • Loading branch information
vitalik authored Dec 7, 2023
2 parents fdc82ee + 838e6ab commit ff26a9b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ninja/orm/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def create_schema(
if key in self.schemas:
return self.schemas[key]

model_fields_list = self._selected_model_fields(model, fields, exclude)
model_fields_list = list(self._selected_model_fields(model, fields, exclude))
if optional_fields:
if optional_fields == "__all__":
optional_fields = [f.name for f in model_fields_list]
Expand Down
15 changes: 14 additions & 1 deletion tests/test_orm_metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def test_optional():
class OptModel(models.Model):
title = models.CharField()
other = models.CharField(null=True)
extra = models.IntegerField()

class Meta:
app_label = "tests"
Expand All @@ -117,9 +118,21 @@ class Meta:
fields = "__all__"
fields_optional = "__all__"

assert OptSchema.json_schema().get("required") is None
assert OptSchema.json_schema().get("required") == ["extra"]
assert OptSchema.json_schema()["properties"] == {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"title": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Title"},
"other": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Other"},
"extra": {"title": "Extra", "type": "integer"},
}

assert OptSchema2.json_schema().get("required") is None
assert OptSchema2.json_schema()["properties"] == {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"title": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Title"},
"other": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Other"},
"extra": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "Extra"},
}


def test_fields_all():
Expand Down

0 comments on commit ff26a9b

Please sign in to comment.