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

dynamic textarea filled with API response on load #9

Open
lsmith77 opened this issue Oct 9, 2023 · 2 comments
Open

dynamic textarea filled with API response on load #9

lsmith77 opened this issue Oct 9, 2023 · 2 comments

Comments

@lsmith77
Copy link

lsmith77 commented Oct 9, 2023

I am building a "rule editor" for NLP purposes. I want to give editors the ability to see "in real time" (after saving), how their rule would work in practice for training sentences they have added to the rule.

ie. a Rule admin form, contains a one2many inline TrainingSentence form

I am hoping to use your package to add a dynamic textarea field "response" that is populated on load with the JSON response from the NLP system for each sentence.

But I am unsure how to construct the DynamicField so that it reads the data from the rule property so that I can call my get_json() helper function to fetch the JSON to set as the initial value for the response dynamic field.

class TrainingSentence(models.Model):
    rule = models.ForeignKey(Rule, on_delete=models.CASCADE)

class TrainingSentenceForm(DynamicFormMixin, forms.ModelForm):
    response = DynamicField(..)

Any advice would be greatly appreciated.

Note: Eventually I might just do this via AJAX, but I am hoping to stick to just backend logic for now.

@lsmith77
Copy link
Author

lsmith77 commented Oct 9, 2023

I have made some progress

    response = DynamicField(
        forms.JSONField,
        initial = get_json(f"/foo?bar")
    )

Now the question is how can I get access to the obj instance.

@lsmith77
Copy link
Author

lsmith77 commented Oct 9, 2023

So I think I have a working solution

class TrainingSentenceForm(DynamicFormMixin, forms.ModelForm):
    response = DynamicField(
        forms.JSONField,
        disabled=True,
        required=False,
        initial=lambda form: None
        if form.initial is None or "text" not in form.initial
        else get_json(f"/tokenize?lang=en&text=" + form.initial["text"]),
    )

Via form.initial["rule"] I can also get the ID of the rule. Not as clean as I hoped, but looks like it will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant