python - Generate code at runtime -
i need generate @ run time model below base on values.
below example of i'm trying acheive, issue clear i.e. [field.value]...
def import_data(form, *args, **kw): class contactcsvmodel(csvmodel): field in form: [field.value] = charfield() class meta: delimiter = "," dbmodel = contact update = {'keys': ["mobile", "group"]} return contactcsvmodel.import_data(*args, **kw)
so above code after generated (if typed static code)....
def import_data(form, *args, **kw): class contactcsvmodel(csvmodel): first_name = charfield() mobile = charfield() last_name = charfield() class meta: delimiter = "," dbmodel = contact update = {'keys': ["mobile", "group"]} return contactcsvmodel.import_data(*args, **kw)
how can [field.value]
work in way need to? i've had looked @ things setattr() don't think i'm after.
forms.py reference...
col_choices = [ ('none', 'no import'), ('first_name', 'first name'), ('last_name', 'last name'), ('company', 'company'), ('mobile', 'mobile number'), ('email', 'email address'), ] class configuratorform(forms.form): col1 = forms.typedchoicefield(choices=col_choices, initial='first_name') col2 = forms.typedchoicefield(choices=col_choices, initial='first_name') col3 = forms.typedchoicefield(choices=col_choices, initial='first_name') col4 = forms.typedchoicefield(choices=col_choices, initial='first_name') col5 = forms.typedchoicefield(choices=col_choices, initial='first_name')
you can add fields class object after it's constructed:
def import_data(form, *args, **kw): class contactcsvmodel(csvmodel): class meta: # ... field in form: setattr(contactcsvmodel, field.value(), charfield()) return contactcsvmodel.import_data(*args, **kw)
Comments
Post a Comment