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

Fix iteration over form fields #16

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
6 changes: 6 additions & 0 deletions django_superform/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ def __init__(self, *args, **kwargs):
super(SuperFormMixin, self).__init__(*args, **kwargs)
self._init_composite_fields()

def __iter__(self):
for name in self.fields:
yield self[name]
for name in self.composite_fields:
yield self[name]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like I'm loose right fields order here. If composite and common fields are interleaved each other in the form definition, the iterator will return them in the wrong order.

def __getitem__(self, name):
"""
Returns a ``django.forms.BoundField`` for the given field name. It also
Expand Down
6 changes: 6 additions & 0 deletions tests/test_boundfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def test_it_is_nonzero_for_forms(self):
self.assertTrue(isinstance(bf, CompositeBoundField))
self.assertEqual(bool(bf), True)

def test_it_iterates_over_own_field(self):
form = AccountForm()
fields = [field.name for field in form]
self.assertEqual(
fields, ['username', 'emails', 'multiple_names', 'nested_form'])

def test_it_iterates_over_form_fields(self):
form = AccountForm()
composite_bf = form['nested_form']
Expand Down