-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Fix a bug in options related to read-only RelatedField
#2981
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
Conversation
rest_framework/metadata.py
Outdated
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
rest_framework/metadata.py
Outdated
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
OPTIONSrequest to an endpoint blew up with the following error:Tracing the problem, I found that
iterableisNonebecause the queryset from which it derives isNonebecause thePrimaryKeyRelatedFieldfrom which it derives is read-only.Solution
It is expected that the
OPTIONSrequest 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_infothat checks if a field is read-only before attempting to get choices