Git Product home page Git Product logo

Comments (5)

bhch avatar bhch commented on May 24, 2024 1

This doesn't seem possible.

The form field and the widget is associated with a model field only once when the django process starts. Therefore, the widget has no way of accessing the model instance.

from django-jsonform.

bhch avatar bhch commented on May 24, 2024 1

Okay, I'll add this. Seems like this is the best that can be done about this feature.

from django-jsonform.

bhch avatar bhch commented on May 24, 2024 1

I've released this feature as per your suggestions in version 2.8.0.

Thank you for this.

from django-jsonform.

martincpt avatar martincpt commented on May 24, 2024

Hi there,

I've just figured out how to implement this.

You are right, by default, it is really not possible to access model instance during the widget render process but it is possible to pass it if you have custom model admin form - which is highly likely - and if not, it's really not hard to create one.

# admin.py
class FooAdminForm(forms.ModelForm):
    class Meta:
        model = Foo
        exclude = []

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['bar'].widget.instance = self.instance # passing instance to the field called bar

@admin.register(Foo)
class FooAdmin(admin.ModelAdmin):
    form = FooAdminForm

So how about giving the possibility for developers to access the instance if they really need it by just take look if there is an instance attribute available in widgets.JSONFormWidget during the render method and pass it to shcema callable if there it is:

# django_jsonform/widgets.py
class JSONFormWidget(forms.Widget):
    # from line 17
    def render(self, name, value, attrs=None, renderer=None):
        if callable(self.schema):
            instance = getattr(self, 'instance') if hasattr(self, 'instance') else None
            schema = self.schema() if instance is None else self.schema(instance)
        else:
            schema = self.schema

from django-jsonform.

martincpt avatar martincpt commented on May 24, 2024

Sounds promising! Thank you!

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.