0

I have a json object like this:

    {  
 "Enzyme": {
        "order": 1,
        "required": "no",
        "help": "enzyme",
        "dataType": "CharField"   },   
"Date": {
        "order": 2,
        "required": "yes",
        "help": "date",
        "dataType": "DateField"   } 
}

And now I want to initialize a form something like this:

class MyForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        for key,values in jsonObj.items():
            self.fields[key] = forms.values["dataType"]

I am not sure how can I do this, as taking this datatype from my json input. What should I write after forms. to initialize forms? Right now it's throwing an error module 'django.forms' has no attribute 'values'

2

1 Answer 1

0

Answer should be: getattr(forms, values["dataType"])

Similar to this one: Accessing models by variable model name in django

This way helps me to create dynamic forms from a json stored in the database.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.