Skip to content

Allow overwriting of get_queryset() of related field #415

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

Merged
merged 2 commits into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion example/tests/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,13 @@ def test_invalid_resource_id_object(self):
}


class BlogResourceRelatedField(ResourceRelatedField):
def get_queryset(self):
return Blog.objects


class BlogFKSerializer(serializers.Serializer):
blog = ResourceRelatedField(queryset=Blog.objects)
blog = BlogResourceRelatedField()


class EntryFKSerializer(serializers.Serializer):
Expand Down
2 changes: 1 addition & 1 deletion rest_framework_json_api/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def to_internal_value(self, data):
if not isinstance(data, dict):
self.fail('incorrect_type', data_type=type(data).__name__)

expected_relation_type = get_resource_type_from_queryset(self.queryset)
expected_relation_type = get_resource_type_from_queryset(self.get_queryset())
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this use of get_queryset() instead of queryset as some of the mixins I've proposed in #416 take advantage of queryset stacking with, for example, self.queryset = super(FilterMixin, self).get_queryset()

Are there other places in the code where self.queryset should be replaced by self.get_queryset() to make this a consistent approach?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have searched through the code and found one more in the documentation and the MultipleIDMixin mixin.

It is adjusted now.

serializer_resource_type = self.get_resource_type_from_included_serializer()

if serializer_resource_type is not None:
Expand Down