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

ManyRelatedField.choices tries to iterate over None #2811

Closed
LilyFoote opened this issue Apr 9, 2015 · 3 comments · Fixed by #2981
Closed

ManyRelatedField.choices tries to iterate over None #2811

LilyFoote opened this issue Apr 9, 2015 · 3 comments · Fixed by #2981

Comments

@LilyFoote
Copy link
Contributor

LilyFoote commented Apr 9, 2015

ManyRelatedField.choices is a property which iterates over self.child_relation.queryset. This can be None if self.child_relation is read-only.
I found this when SimpleMetadata.get_field_info tried to check if field.choices exists and raised TypeError.

@LilyFoote
Copy link
Contributor Author

LilyFoote commented Apr 9, 2015

I'm not sure where the best place to fix this is. A simple fix for my direct issue would be to catch the TypeError in SimpleMetadata.get_field_info, but I'm not sure ManyRelatedField.choices should be raising it in the first place.

@adm271828
Copy link

adm271828 commented Apr 17, 2015

I went into similar issue a few days ago. I got this when making an OPTIONS request and it seems the response tries to iterate over field choices, even if field is read only (which a simple GET resquest doesn't do).

I fixed this by adding (file: relations.py, line:380)

       iterable = queryset.all() if (hasattr(queryset, 'all')) else queryset
+      if not iterable:
+          return { }

This is perhaps a bad behaviour of the OPTION request that could check if field is not read_only before calling choices(). But, on the other hand, the choice method is not robust...

Best regards.

@jannon
Copy link

jannon commented May 28, 2015

I've just run into this as well. I think the solution is definitely to change the behavior of the metadata so that it checks if the field is read_only. After all, there is no point in iterating over the choices for a read_only field. I'll submit a PR shortly

@tomchristie tomchristie added this to the 3.1.3 Release milestone May 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants