Skip to content
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

Cooperation with other repository django-jsonform for widget #55

Closed
sshishov opened this issue Mar 26, 2024 · 5 comments · Fixed by #59
Closed

Cooperation with other repository django-jsonform for widget #55

sshishov opened this issue Mar 26, 2024 · 5 comments · Fixed by #59
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@sshishov
Copy link

sshishov commented Mar 26, 2024

Hi dear maintainer, I am creating this ticket just to notify about possible extension of your repo by cooperating with the following repo: https://github.com/bhch/django-jsonform/

It has the ability to create very functional widgets for JSON schema which is very convenient for forms and django-admins.

Having great FE UI for django-admin and ordinary forms is essential to build functional project. Therefore mentioning or adding HOWTO with example of collaboration of these 2 projects would be beneficial, imho.

@surenkov
Copy link
Owner

@sshishov that's the great idea, agree this is something worth spending the time on integration!

Don't have any good estimates though, there's a #44 which is also long awaited by the devs.

@surenkov
Copy link
Owner

Apparently, at least for simple schemas, it appeared to be working, and I'm impressed with the functionality. Thank you for the suggestion!

The aspect I'm uncertain about is the method for toggling between standard and jsonform widgets.
In PR #59, the approach is contingent on import paths and the presence of django_jsonform in the installed apps. However, overriding the default behavior may not be ideal, and it might be better to specify ModelForm.Meta.field_classes/widgets or ModelAdmin.formfield_overrides directly.

I'm open to discussing this further and would appreciate on any input.

Screenshot 2024-04-01 at 01 38 04 Screenshot 2024-04-01 at 01 38 12

@surenkov
Copy link
Owner

surenkov commented Apr 1, 2024

Played around with it a bit, and came up with pluggable widget overrides, e.g.:

from django.forms import Form
from django_pydantic_field.forms import SchemaField
from django_jsonform.widgets import JSONFormWidget

class SchemaModelForm(Form):
    field = SchemaField(schema=..., widget=JSONFormWidget)

and

from django.contrib import admin
from django_pydantic_field.v2.fields import PydanticSchemaField
from django_jsonform.widgets import JSONFormWidget

@admin.site.register(SchemaModel)
class SchemaModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        PydanticSchemaField: {"widget": JSONFormWidget},
    }

IMHO, this should be a better approach than unconditionally override the default form field behaviours.

@conradlee
Copy link

In the code above what is SchemaModel? It is not defined. Should this be the pydantic schema that we're using for the schemafield?

@surenkov
Copy link
Owner

@conradlee in the following snippet

@admin.site.register(SchemaModel)
class SchemaModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        PydanticSchemaField: {"widget": JSONFormWidget},
    }

SchemaModel refers to a regular Django model that contains SchemaField descriptor(s).
Though these changes aren't published to PyPI -- I thought I'd have a time for one another feature and decided to wait for a new release, which eventually did not happen...

I'll publish them later today as a standalone improvement and update a readme with references to existing models and schemas for clarity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants