Comments (3)
I solved this using widget: hidden
from django-jsonform.
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 django-pydantic-field
and I have a field like this:
my_mapping: Dict[str, str] = Field(default={})
that has no properties/keys pre-defined.
I need the widget working and visible to allow users to edit this value and add str: str
mapping entries.
I think django-jsonform
should support objects with no properties
or keys
defined as long as they have additionalProperties
set.
I've also noticed that if the object is already populated with some user-provided additional parameters that aren't defined in properties
/keys
, they don't show up in the UI at all. This makes it hard to implement these forms to edit Dict[str, str]
because entries "disappear" after the user hits save and refreshes.
Related issue: surenkov/django-pydantic-field#64
from django-jsonform.
As a workaround I've monkey-patched JSONFormWidget
to achieve the desired behavior:
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)
from django-jsonform.
Related Issues (20)
- Custom validation of object properties not showing error next to field HOT 2
- Form dynamics not working after reloading django-jsonform with HTMX HOT 3
- Setting JSONField schema dynamically HOT 3
- Copy-pasting / Duplications for Array items and Objects
- Horizontal display of properties HOT 1
- Dynamic header generation for array items HOT 2
- Customising "Add key" and "Add item" texts HOT 1
- Add button to open link in URLField HOT 5
- Decimal values not allowed when entering integer minimum and maximum for 'number' types HOT 4
- Define defaults for an array HOT 3
- Subfields in ArrayField should get the relevant input type
- Issue with errors for fields inside an anyOf schema not displaying correctly HOT 1
- validators for jsonfield are not updated with dynamic schema HOT 3
- Cooperation with other repository `django-pydantic-field` for model schema validation HOT 1
- Title is not populated for `oneOf` when referenced through `$ref` HOT 3
- tests dir in .venv broke project tests HOT 1
- JSONFieldWidget attrs HOT 1
- Bug: Nullable Subschema (anyOf with `null`) causes UI exception
- Type annotations / stubs
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-jsonform.