From 32472fdaded4309303ea7dee5410cebe5e0b6458 Mon Sep 17 00:00:00 2001 From: Bartek Ciszkowski Date: Tue, 15 Jan 2013 16:43:57 -0500 Subject: [PATCH] Iterate over the form using form.__iter__ to retrieve a more complete field. --- django_remote_forms/fields.py | 18 +++++++++++------- django_remote_forms/forms.py | 18 +++++++++--------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/django_remote_forms/fields.py b/django_remote_forms/fields.py index 9c6adee..22aaf87 100644 --- a/django_remote_forms/fields.py +++ b/django_remote_forms/fields.py @@ -16,20 +16,24 @@ class RemoteField(object): specified on the field per Django's rules: https://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values + + `bound_field` is the BoundField returned from iterating over the form. """ - def __init__(self, field, form_initial_data=None, field_name=None): - self.field_name = field_name - self.field = field + def __init__(self, bound_field, form_initial_data=None): + self.name = bound_field.name + self.label = bound_field.label + self.help_text = bound_field.help_text + self.field = bound_field.field self.form_initial_data = form_initial_data def as_dict(self): field_dict = SortedDict() - field_dict['title'] = self.field.__class__.__name__ + field_dict['title'] = self.name field_dict['required'] = self.field.required - field_dict['label'] = self.field.label + field_dict['label'] = self.label field_dict['initial'] = self.form_initial_data or self.field.initial - field_dict['help_text'] = self.field.help_text + field_dict['help_text'] = self.help_text field_dict['error_messages'] = self.field.error_messages @@ -38,7 +42,7 @@ def as_dict(self): remote_widget_class_name = 'Remote%s' % self.field.widget.__class__.__name__ try: remote_widget_class = getattr(widgets, remote_widget_class_name) - remote_widget = remote_widget_class(self.field.widget, field_name=self.field_name) + remote_widget = remote_widget_class(self.field.widget, field_name=self.name) except Exception, e: logger.warning('Error serializing %s: %s', remote_widget_class_name, str(e)) widget_dict = {} diff --git a/django_remote_forms/forms.py b/django_remote_forms/forms.py index e9c6c16..34255f9 100644 --- a/django_remote_forms/forms.py +++ b/django_remote_forms/forms.py @@ -111,7 +111,7 @@ def as_dict(self): initial_data = {} - for name, field in [(x, self.form.fields[x]) for x in self.fields]: + for field in (x for x in self.form if x.name in self.fields): # Retrieve the initial data from the form itself if it exists so # that we properly handle which initial data should be returned in # the dictionary. @@ -119,30 +119,30 @@ def as_dict(self): # Please refer to the Django Form API documentation for details on # why this is necessary: # https://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values - form_initial_field_data = self.form.initial.get(name) + form_initial_field_data = self.form.initial.get(field.name) # Instantiate the Remote Forms equivalent of the field if possible # in order to retrieve the field contents as a dictionary. - remote_field_class_name = 'Remote%s' % field.__class__.__name__ + remote_field_class_name = 'Remote%s' % field.field.__class__.__name__ try: remote_field_class = getattr(fields, remote_field_class_name) - remote_field = remote_field_class(field, form_initial_field_data, field_name=name) + remote_field = remote_field_class(field, form_initial_field_data) except Exception, e: logger.warning('Error serializing field %s: %s', remote_field_class_name, str(e)) field_dict = {} else: field_dict = remote_field.as_dict() - if name in self.readonly_fields: + if field.name in self.readonly_fields: field_dict['readonly'] = True - form_dict['fields'][name] = field_dict + form_dict['fields'][field.name] = field_dict # Load the initial data, which is a conglomerate of form initial and field initial - if 'initial' not in form_dict['fields'][name]: - form_dict['fields'][name]['initial'] = None + if 'initial' not in form_dict['fields'][field.name]: + form_dict['fields'][field.name]['initial'] = None - initial_data[name] = form_dict['fields'][name]['initial'] + initial_data[field.name] = form_dict['fields'][field.name]['initial'] if self.form.data: form_dict['data'] = self.form.data