I think I figured it out. My TYPE_MAPPING thing was causing the form fields to be initialized *once*. So, I made the contents of TYPE_MAPPING into strings, and I'm using eval().
Unless there's a better way, I'm leaving it like this in production :-) On Mar 9, 10:01 am, Steven Smith <ste...@stevenlsmith.com> wrote: > I have data that looks like this: > formfields = [{'label': u'IP address of hacker', 'type': u'String', > 'name': u'locationIp'}, {'label': u'Created end date', 'type': > u'Date', 'name': u'createdEndDate'}, {'label': u'Created start date', > 'type': u'Date', 'name': u'createdStartDate'}] > > I want to generate a forms.Form object from it, so I wrote the > following code (ReportForm is just an empty declaration of a form > class): > > --------- > > TYPE_MAPPING = { > 'Date': forms.DateTimeField(required=True, > widget=forms.DateTimeInput({'class': 'datetime'})), > 'String': forms.CharField(max_length=100, required=True), > 'Long': forms.IntegerField(required=False), > 'Integer': forms.IntegerField(required=False), > 'Boolean': forms.BooleanField(required=False), > > } > > def get_form_for_report(formfields, postdata=None): > if postdata: > form = ReportForm(postdata) > else: > form = ReportForm() > attrs = formfields.reverse() > for f in attrs: > form.fields.insert(1, f['name'], TYPE_MAPPING.get(f['type'])) > form.fields[f['name']].label = f['label'] > return form > > --------- > > However, when this code executes, the form generated works perfectly, > but the label is not always set properly. In this particular example, > I get two fields (with correct field names) that have the label > "Created Start Date". > > Where I am going wrong here? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.