Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterate over the form using form.__iter__ to retrieve a more complete field #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions django_remote_forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = {}
Expand Down
18 changes: 9 additions & 9 deletions django_remote_forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,38 +111,38 @@ 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.

# 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
Expand Down