Why don't these class attributes register?
Posted
by slypete
on Stack Overflow
See other posts from Stack Overflow
or by slypete
Published on 2010-04-08T23:55:32Z
Indexed on
2010/04/09
0:43 UTC
Read the original article
Hit count: 425
I have a factory method that generates django form classes like so:
def get_indicator_form(indicator, patient):
class IndicatorForm(forms.Form):
#These don't work!
indicator_id = forms.IntegerField(initial=indicator.id, widget=forms.HiddenInput())
patient_id = forms.IntegerField(initial=patient.id, widget=forms.HiddenInput())
def __init__(self, *args, **kwargs):
forms.Form.__init__(self, *args, **kwargs)
self.indicator = indicator
self.patient = patient
#These do!
setattr(IndicatorForm, 'indicator_id', forms.IntegerField(initial=indicator.id, widget=forms.HiddenInput()))
setattr(IndicatorForm, 'patient_id', forms.IntegerField(initial=patient.id, widget=forms.HiddenInput()))
for field in indicator.indicatorfield_set.all():
setattr(IndicatorForm, field.name, copy(field.get_field_type()))
return type('IndicatorForm', (forms.Form,), dict(IndicatorForm.__dict__))
I'm trying to understand why the top form field declarations don't work, but the setattr method below does work. I'm fairly new to python, so I suspect it's some language feature that I'm misunderstanding. Can you help me understand why the field declarations at the top of the class don't add the fields to the class?
In a possibly related note, when these classes are instantiated, instance.media
returns nothing even though some fields have widgets with associated media.
Thanks, Pete
© Stack Overflow or respective owner