gregmuellegger / django-superform

Add forms and formsets to other forms like they were fields.

Home Page:http://django-superform.readthedocs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

__getitem__ in ModelField does not work

duduklein opened this issue · comments

Hi,

First of all, thank you for this package. It's really good and useful.

I have found a bug though:
In the FormField's example , the {{ registration_form.address.street }} does not work.

In python, If I do:

from django import forms
from django_superform import SuperForm, FormField

class AddressForm(forms.Form):
    street = forms.CharField()
    city = forms.CharField()

class RegistrationForm(SuperForm):
    first_name = forms.CharField()
    last_name = forms.CharField()
    address = FormField(AddressForm)

rf = RegistrationForm()
ad = rf["address"]
ad["street"]

I get

local/lib/python2.7/site-packages/django/forms/forms.pyc in __getitem__(self, idx)
    556         # from templates.
    557         if not isinstance(idx, six.integer_types):
--> 558             raise TypeError
    559         return list(self.__iter__())[idx]
    560 

TypeError: 

In the code, there is a comment stating that overriding getitem is not necessary, but it seems it is according to my tests. Iterating over the FormField works fine (the __iter__ is overridden.

Thanks in advance

Yep, you're absolutely right and identified the right place to fix this. I've added the CompositeBoundField.__getitem__ method, now it works as advertised. Thanks for the report!

I just released 0.3.0 containing that fix.