-
-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using an object with empty Properties, having all properties inside AdditionalProperties #144
Comments
I solved this using widget: hidden |
I think this should be re-opened as the original use-case should still be supported by the library without a workaround. In my case I'm using I need the widget working and visible to allow users to edit this value and add I think I've also noticed that if the object is already populated with some user-provided additional parameters that aren't defined in Related issue: surenkov/django-pydantic-field#64 |
As a workaround I've monkey-patched from django.contrib import admin
from django_jsonform.widgets import JSONFormWidget
from django_pydantic_field.v2.fields import PydanticSchemaField
from project.models import Dependency
def patch_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/keys
if schema.get('type') == 'object' and not ('properties' in schema or 'keys' in schema):
if 'default' in schema and isinstance(schema['default'], dict):
schema['properties'] = {
key: {"type": "string", "default": value}
for key, value in schema['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 up
else:
schema['properties'] = {}
# recursive case: iterate through all values and process any sub-objects
for key, value in schema.items():
if isinstance(value, dict):
patch_schema_for_jsonform(value)
class PatchedJSONFormWidget(JSONFormWidget):
def get_schema(self):
self.schema = super().get_schema()
patch_schema_for_jsonform(self.schema)
return self.schema
class DependencyAdmin(admin.ModelAdmin):
formfield_overrides = {PydanticSchemaField: {"widget": PatchedJSONFormWidget}}
admin.site.register(Dependency, DependencyAdmin) |
I have a usage case in which I need the user to define a set of custom properties. I do not want nor need any normal properties. Adding any would just be unnecessary and obtrusive. Is it possible to make an object with empty "properties" or at least hide a fields inside of that for the end user, as a possible fix?
The text was updated successfully, but these errors were encountered: