You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This field contains a mapping of str to str and I Want users to be able to add their own entries , but the default contains no pre-defined keys/properties, so it throws an error right now:
Error: Error while creating EditorState: Invalid schema: Schema of type 'object' must have at least one of these keys: ['properties' or 'keys' or 'oneOf' or 'anyOf' or 'allOf']
I think the issue can be fixed by adding an empty properties: {} entry to the schema generation /conversion code before it gets passed to django-jsonform: MySchemaModel().model_json_schema()
I think django-jsonform should also natively support objects with no properties/keys defined if they have additionalProperties set. I commented on a related an issue on their side here: bhch/django-jsonform#144.
The text was updated successfully, but these errors were encountered:
As a workaround I've monkey-patched JSONFormWidget to achieve the desired behavior:
fromdjango.contribimportadminfromdjango_jsonform.widgetsimportJSONFormWidgetfromdjango_pydantic_field.v2.fieldsimportPydanticSchemaFieldfromproject.modelsimportDependencydefpatch_schema_for_jsonform(schema):
"""recursively patch a schema dictionary in-place to fix any missing properties/keys on objects"""# base case: schema is type: "object" with no properties/keysifschema.get('type') =='object'andnot ('properties'inschemaor'keys'inschema):
if'default'inschemaandisinstance(schema['default'], dict):
schema['properties'] = {
key: {"type": "string", "default": value}
forkey, valueinschema['default'].items()
}
# setting the actual value as a default on a hardcoded property is still not ideal as it doesn't allow the user to remove this entry from the UI, but at least it shows upelse:
schema['properties'] = {}
# recursive case: iterate through all values and process any sub-objectsforkey, valueinschema.items():
ifisinstance(value, dict):
patch_schema_for_jsonform(value)
classPatchedJSONFormWidget(JSONFormWidget):
defget_schema(self):
self.schema=super().get_schema()
patch_schema_for_jsonform(self.schema)
returnself.schemaclassDependencyAdmin(admin.ModelAdmin):
formfield_overrides= {PydanticSchemaField: {"widget": PatchedJSONFormWidget}}
admin.site.register(Dependency, DependencyAdmin)
I have a pydantic model that has a field like so:
This field contains a mapping of
str
tostr
and I Want users to be able to add their own entries , but the default contains no pre-defined keys/properties, so it throws an error right now:I think the issue can be fixed by adding an empty
properties: {}
entry to the schema generation /conversion code before it gets passed todjango-jsonform
:MySchemaModel().model_json_schema()
{ "properties": { "my_mapping": { "additionalProperties": { "type": "string" }, "default": {}, "title": "My Mapping", + "properties": {}, "type": "object" } }, "title": "MySchemaModel", "type": "object" }
I think
django-jsonform
should also natively support objects with noproperties
/keys
defined if they haveadditionalProperties
set. I commented on a related an issue on their side here: bhch/django-jsonform#144.The text was updated successfully, but these errors were encountered: