@@ -33,12 +33,12 @@ class Meta:
3333 "title" : "ChildModel" ,
3434 "type" : "object" ,
3535 "properties" : {
36- "id" : {"anyOf " : [{ "type" : "integer" }, { "type" : "null" }], "title" : "ID " },
36+ "id" : {"title " : "ID" , "type" : "integer " },
3737 "parent_field" : {"type" : "string" , "title" : "Parent Field" },
3838 "parentmodel_ptr_id" : {"type" : "integer" , "title" : "Parentmodel Ptr" },
3939 "child_field" : {"type" : "string" , "title" : "Child Field" },
4040 },
41- "required" : ["parent_field" , "parentmodel_ptr_id" , "child_field" ],
41+ "required" : ["id" , " parent_field" , "parentmodel_ptr_id" , "child_field" ],
4242 }
4343
4444
@@ -87,7 +87,7 @@ class Meta:
8787 "title" : "AllFields" ,
8888 "type" : "object" ,
8989 "properties" : {
90- "id" : {"anyOf " : [{ "type" : "integer" }, { "type" : "null" }], "title" : "ID " },
90+ "id" : {"title " : "ID" , "type" : "integer " },
9191 "bigintegerfield" : {"title" : "Bigintegerfield" , "type" : "integer" },
9292 "binaryfield" : {
9393 "title" : "Binaryfield" ,
@@ -160,6 +160,7 @@ class Meta:
160160 }),
161161 },
162162 "required" : [
163+ "id" ,
163164 "bigintegerfield" ,
164165 "binaryfield" ,
165166 "booleanfield" ,
@@ -212,10 +213,7 @@ class Meta:
212213 SchemaCls = create_schema (ModelAltAuto )
213214 # print(SchemaCls.json_schema())
214215 assert SchemaCls .json_schema ()["properties" ] == {
215- "altautofield" : {
216- "anyOf" : [{"type" : "integer" }, {"type" : "null" }],
217- "title" : "Altautofield" ,
218- }
216+ "altautofield" : {"title" : "Altautofield" , "type" : "integer" }
219217 }
220218
221219
@@ -233,14 +231,14 @@ class Meta:
233231 "title" : "ModelNewFields" ,
234232 "type" : "object" ,
235233 "properties" : {
236- "id" : {"title" : "ID" , "anyOf" : [{ " type" : "integer" }, { "type" : "null" }] },
234+ "id" : {"title" : "ID" , "type" : "integer" },
237235 "jsonfield" : {"title" : "Jsonfield" , "type" : "object" },
238236 "positivebigintegerfield" : {
239237 "title" : "Positivebigintegerfield" ,
240238 "type" : "integer" ,
241239 },
242240 },
243- "required" : ["jsonfield" , "positivebigintegerfield" ],
241+ "required" : ["id" , " jsonfield" , "positivebigintegerfield" ],
244242 }
245243
246244 obj = Schema (id = 1 , jsonfield = {"any" : "data" }, positivebigintegerfield = 1 )
@@ -272,7 +270,7 @@ class Meta:
272270 "title" : "TestSchema" ,
273271 "type" : "object" ,
274272 "properties" : {
275- "id" : {"anyOf " : [{ "type" : "integer" }, { "type" : "null" }], "title" : "ID " },
273+ "id" : {"title " : "ID" , "type" : "integer " },
276274 "onetoonefield_id" : {"title" : "Onetoonefield" , "type" : "integer" },
277275 "foreignkey_id" : {
278276 "anyOf" : [{"type" : "integer" }, {"type" : "null" }],
@@ -284,15 +282,15 @@ class Meta:
284282 "items" : {"type" : "integer" },
285283 },
286284 },
287- "required" : ["onetoonefield_id" , "manytomanyfield" ],
285+ "required" : ["id" , " onetoonefield_id" , "manytomanyfield" ],
288286 }
289287
290288 SchemaClsDeep = create_schema (TestModel , name = "TestSchemaDeep" , depth = 1 )
291289 print (SchemaClsDeep .json_schema ())
292290 assert SchemaClsDeep .json_schema () == {
293291 "type" : "object" ,
294292 "properties" : {
295- "id" : {"anyOf " : [{ "type" : "integer" }, { "type" : "null" }], "title" : "ID " },
293+ "id" : {"title " : "ID" , "type" : "integer " },
296294 "onetoonefield" : pydantic_ref_fix ({
297295 "title" : "Onetoonefield" ,
298296 "description" : "" ,
@@ -310,20 +308,17 @@ class Meta:
310308 "description" : "" ,
311309 },
312310 },
313- "required" : ["onetoonefield" , "manytomanyfield" ],
311+ "required" : ["id" , " onetoonefield" , "manytomanyfield" ],
314312 "title" : "TestSchemaDeep" ,
315313 "$defs" : {
316314 "Related" : {
317315 "title" : "Related" ,
318316 "type" : "object" ,
319317 "properties" : {
320- "id" : {
321- "anyOf" : [{"type" : "integer" }, {"type" : "null" }],
322- "title" : "ID" ,
323- },
318+ "id" : {"title" : "ID" , "type" : "integer" },
324319 "charfield" : {"type" : "string" , "title" : "Charfield" },
325320 },
326- "required" : ["charfield" ],
321+ "required" : ["id" , " charfield" ],
327322 }
328323 },
329324 }
@@ -343,14 +338,15 @@ class Meta:
343338 "title" : "MyModel" ,
344339 "type" : "object" ,
345340 "properties" : {
346- "id" : {"anyOf " : [{ "type" : "integer" }, { "type" : "null" }], "title" : "ID " },
341+ "id" : {"title " : "ID" , "type" : "integer " },
347342 "default_static" : {
348343 "default" : "hello" ,
349344 "title" : "Default Static" ,
350345 "type" : "string" ,
351346 },
352347 "default_dynamic" : {"title" : "Default Dynamic" , "type" : "string" },
353348 },
349+ "required" : ["id" ],
354350 }
355351
356352
@@ -392,11 +388,11 @@ class Meta:
392388 assert Schema3 .json_schema () == {
393389 "type" : "object" ,
394390 "properties" : {
395- "id" : {"anyOf " : [{ "type" : "integer" }, { "type" : "null" }], "title" : "ID " },
391+ "id" : {"title " : "ID" , "type" : "integer " },
396392 "f1" : {"type" : "string" , "title" : "F1" },
397393 "f2" : {"type" : "string" , "title" : "F2" },
398394 },
399- "required" : ["f1" , "f2" ],
395+ "required" : ["id" , " f1" , "f2" ],
400396 "title" : "SampleModel3" ,
401397 }
402398
@@ -441,10 +437,10 @@ def test_with_relations():
441437 "title" : "Category" ,
442438 "type" : "object" ,
443439 "properties" : {
444- "id" : {"anyOf " : [{ "type" : "integer" }, { "type" : "null" }], "title" : "ID " },
440+ "id" : {"title " : "ID" , "type" : "integer " },
445441 "title" : {"title" : "Title" , "maxLength" : 100 , "type" : "string" },
446442 },
447- "required" : ["title" ],
443+ "required" : ["id" , " title" ],
448444 }
449445
450446
@@ -495,12 +491,12 @@ class Meta:
495491 assert Schema1 .json_schema () == {
496492 "type" : "object" ,
497493 "properties" : {
498- "id" : {"anyOf " : [{ "type" : "integer" }, { "type" : "null" }], "title" : "ID " },
494+ "id" : {"title " : "ID" , "type" : "integer " },
499495 "f1" : {"type" : "string" , "title" : "F1" },
500496 "f2" : {"type" : "string" , "title" : "F2" },
501497 "custom" : {"type" : "integer" , "title" : "Custom" },
502498 },
503- "required" : ["f1" , "f2" , "custom" ],
499+ "required" : ["id" , " f1" , "f2" , "custom" ],
504500 "title" : "SmallModel" ,
505501 }
506502
@@ -510,11 +506,11 @@ class Meta:
510506 assert Schema2 .json_schema () == {
511507 "type" : "object" ,
512508 "properties" : {
513- "id" : {"anyOf " : [{ "type" : "integer" }, { "type" : "null" }], "title" : "ID " },
509+ "id" : {"title " : "ID" , "type" : "integer " },
514510 "f1" : {"type" : "integer" , "title" : "F1" },
515511 "f2" : {"type" : "string" , "title" : "F2" },
516512 },
517- "required" : ["f1" , "f2" ],
513+ "required" : ["id" , " f1" , "f2" ],
518514 "title" : "SmallModel2" ,
519515 }
520516
@@ -573,15 +569,35 @@ class Meta:
573569 app_label = "tests"
574570
575571 Schema = create_schema (SomeReqFieldModel )
576- assert Schema .json_schema ()["required" ] == ["some_field" , "other_field" ]
572+ assert Schema .json_schema ()["required" ] == ["id" , " some_field" , "other_field" ]
577573
578574 Schema = create_schema (SomeReqFieldModel , optional_fields = ["some_field" ])
579- assert Schema .json_schema ()["required" ] == ["other_field" ]
575+ assert Schema .json_schema ()["required" ] == ["id" , " other_field" ]
580576
581577 Schema = create_schema (
582578 SomeReqFieldModel , optional_fields = ["some_field" , "other_field" , "optional" ]
583579 )
584- assert Schema .json_schema ().get ("required" ) is None
580+ assert Schema .json_schema ()["required" ] == ["id" ]
581+
582+
583+ def test_optional_primary_key ():
584+ class ModelWithOptionalPK (models .Model ):
585+ id = models .AutoField (primary_key = True , null = True )
586+ name = models .CharField ()
587+
588+ class Meta :
589+ app_label = "tests"
590+
591+ Schema = create_schema (ModelWithOptionalPK )
592+ assert Schema .json_schema () == {
593+ "properties" : {
594+ "id" : {"anyOf" : [{"type" : "integer" }, {"type" : "null" }], "title" : "Id" },
595+ "name" : {"title" : "Name" , "type" : "string" },
596+ },
597+ "required" : ["name" ],
598+ "title" : "ModelWithOptionalPK" ,
599+ "type" : "object" ,
600+ }
585601
586602
587603def test_register_custom_field ():
0 commit comments