Git Product home page Git Product logo

Comments (3)

ErikKoinberg avatar ErikKoinberg commented on June 16, 2024

I solved this using widget: hidden

from django-jsonform.

pirate avatar pirate commented on June 16, 2024

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.

pirate avatar pirate commented on June 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.