Git Product home page Git Product logo

Comments (4)

bhch avatar bhch commented on September 23, 2024 2

Okay, everything is clear now. I will provide a way to pass extra arguments to the callable in the next release. Probably introduce a new attribute called schema_kwargs. Something like this:

# declare extra arguments to pass to the callable schema
self.fields['some_json'].widget.schema_kwargs = {'key': value, ...}

Meanwhile, let me suggest a simpler hack for your use case.

The widget doesn't care what the value of the instance attribute is. You can set it to whatever value you want and it will be passed to the callable, i.e. you can treat it as a vehicle to carry any kind of data:

# form
class MyModelForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fields["some_json"].widget.instance = {
            'instance': self.instance,
            'some_property': kwargs['initial']['some_property']
        }


# callable
def callable_config_schema(data):
    """
    data is a dict containing these keys:
        - instance: instance of the model
        - some_property: value of some_property field
    """"
    if data['instance'] is not None:
        # model instance is present
       ...
    else:
        # no instance found, so use data['some_property'] value
        ... 

from django-jsonform.

bhch avatar bhch commented on September 23, 2024

Hi, I think I kind of understand what you're proposing.

But if you could also provide a minimal example of the model and your callable schema function, it will be a bit clearer to me.

from django-jsonform.

corradio avatar corradio commented on September 23, 2024

Hello,
I've given a more complete example below, alongside my current (hacky) solution. I hope this clarifies things.

def schema_getter(some_property):
    # .... <fetches schema based on value of `some_property`>

# Model
class MyModel(models.Model):
    some_property = models.CharField(max_length=128)
    def callable_config_schema(model_instance=None):
        # Here it would be great to be able to know what the initial value of
        # `some_property` is, as the model instance hasn't been instantiated yet
        # for CreateView
        if model_instance: return schema_getter(model_instance.some_property)
    some_json = JSONField(schema=callable_config_schema)

# Form
class MyModelForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # This is my hack to make sure the schema is correctly selected
        if kwargs.get("initial", {}).get("some_property"):
            self.fields["some_json"].widget.schema = self.fields[
                "some_json"
            ].widget.schema(MyModel(integration_id=kwargs["initial"]["some_property"]))
        # manually set the current instance on the widget
        # see https://django-jsonform.readthedocs.io/en/latest/fields-and-widgets.html#accessing-model-instance-in-callable-schema
        self.fields["some_json"].widget.instance = self.instance
    class Meta:
        model = MyModel
        fields = "__all__"

# View
class MyCreateView(CreateView):
    model = MyModel
    form_class = MyModelForm
    def get_initial(self):
        # self.kwargs contains parameters from the url
        return {'some_property': self.kwargs['some_property']}

In urls.py:

urlpatterns = [
    path(
        "some/path/<some_property>/",
        views.MyCreateView.as_view()
    ),
    ....

from django-jsonform.

corradio avatar corradio commented on September 23, 2024

Good idea - thanks!

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.