Skip to content

Error: 'BoundField' object is not iterable in Python3 #3153

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

Closed
vstoykov opened this issue Jul 15, 2015 · 11 comments
Closed

Error: 'BoundField' object is not iterable in Python3 #3153

vstoykov opened this issue Jul 15, 2015 · 11 comments
Assignees
Labels
Milestone

Comments

@vstoykov
Copy link
Contributor

When I use Python3 an have nested model serializers I see this error:

'BoundField' object is not iterable

I made a little investigation and found that BoundField proxy does not work for some reason.

I run in Python console (both Python2 and Python3) this example code:

 class I():
     def __iter__(self):
         for i in range(5):
             yield i

class BoundI():
    def __init__(self, i):
        self._i = i
    def __getattr__(self, attr):
        return getattr(self._i, attr)

i = I()
bi = BoundI(i)

list(i)  # work
list(bi)  # work only on Python2. In Py3 the error is "object is not iterable"

These example classes use the same logic as Serialzier and BoundField classes in DRF.

@vstoykov
Copy link
Contributor Author

Hmm now I see that there is a NestedBoundField, but in my case for some reason is created BoundField instead of NestedBoundField.

I will continue with investigation.

@xordoquy
Copy link
Collaborator

A bit more context would help, in particular why do you have a BoundField.
You'd usually want to use the serializer's validated_data instead.

@vstoykov
Copy link
Contributor Author

The problem is caused by this commit 6b08e97

BoundField.as_form_field allways returns BoundField and then when this method is called on NestedBoundField instance then instance of BoundField is returned. When DRF renderds the field with it's template https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/templates/rest_framework/horizontal/fieldset.html then in the {% for nested_field in field %} the exception is raised.

@tomchristie can you look at this because you implemented as_form_field and probably you have something in mind.

@tomchristie
Copy link
Member

Yup sounds like a bug against master as part of resolving some other stuff. I'll have a dig.
If you're able to include the smallest possible set of code that could replicate this that'd be wonderful, but no worries if not.

@tomchristie tomchristie self-assigned this Jul 15, 2015
@xordoquy xordoquy added this to the 3.1.4 Release milestone Jul 15, 2015
@vstoykov
Copy link
Contributor Author

I will try to write something tomorrow.

This affects only browsable API. If you just get JSON responses via AJAX, everything is working normally.

@tomchristie
Copy link
Member

Now resolved. Thanks for the report!

@vstoykov
Copy link
Contributor Author

I think that it is not. Now it raises 'str' object has no attribute 'get'. I will check first if this is related to my code but I think that this is not the reason .

@tomchristie
Copy link
Member

Noted. Shame we don't yet have test coverage over this.

@tomchristie
Copy link
Member

Okay, looks sane to me now. Worth verifying @vstoykov?

vstoykov added a commit to vstoykov/django-rest-framework that referenced this issue Jul 16, 2015
@vstoykov
Copy link
Contributor Author

It's ok now. I've created a test that will fail if executed before last change. After the last change is Ok. You can look at it and if you are ok with it I can make a pull request.

vstoykov@20e0e68

@tomchristie
Copy link
Member

@vstoykov That would probably be a good plan - thanks!

vstoykov added a commit to vstoykov/django-rest-framework that referenced this issue Jul 16, 2015
tomchristie added a commit that referenced this issue Jul 16, 2015
@tomchristie tomchristie modified the milestones: 3.1.4 Release, 3.2.0 Release Jul 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants