-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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 a bug in options related to read-only RelatedField
#2981
Conversation
} | ||
for choice_value, choice_name in field.choices.items() | ||
] | ||
if 'read_only' in field_info and not field_info['read_only']: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we instead do if not field_info.get('read_only'): ...
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
8fd2585
to
9b22f43
Compare
for choice_value, choice_name in field.choices.items() | ||
] | ||
if not field_info.get('read_only'): | ||
if hasattr(field, 'choices'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In theory can't these two be combined?
if not field_info.get('read_only') and hasattr(field, 'choices'):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seconded
9b22f43
to
a97c989
Compare
Looks good to me. Thanks !! |
Read-Only RelatedField Metadata Failure Test and Fix
RelatedField
in OPTIONS
RelatedField
in OPTIONSRelatedField
This fixes #2811
Problem
The issue was discovered when an
OPTIONS
request to an endpoint blew up with the following error:Tracing the problem, I found that
iterable
isNone
because the queryset from which it derives isNone
because thePrimaryKeyRelatedField
from which it derives is read-only.Solution
It is expected that the
OPTIONS
request should succeed and simply not list choices for read-only fields since, a user cannot submit data to them anyway.This pull request contains both a test that fails on existing code and a fix in
SimpleMetaData.get_field_info
that checks if a field is read-only before attempting to get choices