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

Document the use of get_queryset for RelatedField #3605

Merged
merged 5 commits into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api-guide/relations.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ To implement a custom relational field, you should override `RelatedField`, and

If you want to implement a read-write relational field, you must also implement the `.to_internal_value(self, data)` method.

To provide a dynamic queryset based on the `context`, you can also override `.get_queryset(self)` instead of specifying `.queryset` on the class or when initializing the field.

## Example

For example, we could define a relational field to serialize a track to a custom string representation, using its ordering, title, and duration.
Expand Down
19 changes: 15 additions & 4 deletions rest_framework/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
from rest_framework.utils import html


def method_overridden(method_name, klass, instance):
"""
Determine if a method has been overridden.
"""
method = getattr(klass, method_name)
default_method = getattr(method, '__func__', method) # Python 3 compat
return default_method is not getattr(instance, method_name).__func__


class Hyperlink(six.text_type):
"""
A string like object that additionally has an associated name.
Expand Down Expand Up @@ -62,10 +71,12 @@ def __init__(self, **kwargs):
self.queryset = kwargs.pop('queryset', self.queryset)
self.html_cutoff = kwargs.pop('html_cutoff', self.html_cutoff)
self.html_cutoff_text = kwargs.pop('html_cutoff_text', self.html_cutoff_text)
assert self.queryset is not None or kwargs.get('read_only', None), (
'Relational field must provide a `queryset` argument, '
'or set read_only=`True`.'
)

if not method_overridden('get_queryset', RelatedField, self):
assert self.queryset is not None or kwargs.get('read_only', None), (
'Relational field must provide a `queryset` argument, '
'override `get_queryset`, or set read_only=`True`.'
)
assert not (self.queryset is not None and kwargs.get('read_only', None)), (
'Relational fields should not provide a `queryset` argument, '
'when setting read_only=`True`.'
Expand Down
8 changes: 8 additions & 0 deletions tests/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ def test_representation(self):
representation = self.field.to_representation(self.instance)
assert representation == self.instance.name

def test_no_queryset_init(self):
class NoQuerySetSlugRelatedField(serializers.SlugRelatedField):
def get_queryset(this):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be self instead of this ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ticosax it indeed should be.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not, I need to dig this a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because I needed to get to self from the test case inside this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It perhaps would have been better to use staticmethod, or at least add a comment about why it was like that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored slightly in #3861

return self.queryset

field = NoQuerySetSlugRelatedField(slug_field='name')
field.to_internal_value(self.instance.name)


class TestManyRelatedField(APISimpleTestCase):
def setUp(self):
Expand Down