Skip to content

Commit 628b2be

Browse files
committed
add to inheritance usage test
1 parent d8ae48d commit 628b2be

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

tests/test_orm_metaclass.py

+24-16
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import pytest
44
from django.db import models
5-
from pydantic import ValidationError
5+
from pydantic import BaseModel, ValidationError
66

7-
from ninja import ModelSchema
7+
from ninja import ModelSchema, Schema
88
from ninja.errors import ConfigError
99

1010

@@ -245,19 +245,28 @@ class Meta:
245245

246246
def test_desired_inheritance():
247247
class Item(models.Model):
248-
id = models.PositiveIntegerField
248+
id = models.PositiveIntegerField(primary_key=True)
249249
slug = models.CharField()
250250

251251
class Meta:
252252
app_label = "tests"
253253

254-
class ProjectModelSchema(ModelSchema):
254+
class ProjectBaseSchema(Schema):
255+
# add any project wide Schema/pydantic configs
256+
_omissible_serialize = (
257+
"serializer_func" # model_serializer(mode="wrap")(_omissible_serialize)
258+
)
259+
260+
class ProjectBaseModelSchema(ModelSchema, ProjectBaseSchema):
255261
_pydantic_config = "config"
256262

257-
class ResourceModelSchema(ProjectModelSchema):
263+
class Meta:
264+
primary_key_optional = False
265+
266+
class ResourceModelSchema(ProjectBaseModelSchema):
258267
field1: str
259268

260-
class Meta:
269+
class Meta(ProjectBaseModelSchema.Meta):
261270
model = Item
262271
fields = ["id"]
263272

@@ -268,25 +277,23 @@ class Meta(ResourceModelSchema.Meta):
268277
model = Item
269278
fields = ["id", "slug"]
270279

280+
assert issubclass(ItemModelSchema, BaseModel)
281+
assert ItemModelSchema.Meta.primary_key_optional is False
282+
271283
i = ItemModelSchema(id=1, slug="slug", field1="1", field2="2")
284+
272285
assert i._pydantic_config == "config"
286+
assert i._omissible_serialize == "serializer_func"
287+
assert i.model_dump_json() == '{"field1":"1","id":1,"field2":"2","slug":"slug"}'
273288
assert i.model_json_schema() == {
274289
"properties": {
275290
"field1": {
276291
"title": "Field1",
277292
"type": "string",
278293
},
279294
"id": {
280-
"anyOf": [
281-
{
282-
"type": "integer",
283-
},
284-
{
285-
"type": "null",
286-
},
287-
],
288-
"default": None,
289-
"title": "ID",
295+
"type": "integer",
296+
"title": "Id",
290297
},
291298
"field2": {
292299
"title": "Field2",
@@ -299,6 +306,7 @@ class Meta(ResourceModelSchema.Meta):
299306
},
300307
"required": [
301308
"field1",
309+
"id",
302310
"field2",
303311
"slug",
304312
],

0 commit comments

Comments
 (0)