Skip to content

ResourceRelatedField now accepts serializer methods which return querysets. #151

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

Closed
wants to merge 3 commits into from

Conversation

sonthonaxrk
Copy link
Contributor

Specifying the source of a field to be a method on a serializer, only works if many=False. This pull request allows you to use serializer methods for ResourceRelatedField which return multiple objects.

Which allows you to do things like this:

class EntrySerializer(serializers.ModelSerializer):
    included_serializers = {
        'approved_comments': 'example.serializers.EntrySerializer',
    }
    approved_comments = relations.ResourceRelatedField(source='get_approved_comments',
            many=True, model=Comment, read_only=True)

    def get_approved_comments(self, obj):
        return self.comments.approved()

@sonthonaxrk sonthonaxrk changed the title ResourceRelatedField now accepts serializer methods when many=True ResourceRelatedField now accepts serializer methods the return querysets. Nov 4, 2015
@sonthonaxrk sonthonaxrk changed the title ResourceRelatedField now accepts serializer methods the return querysets. ResourceRelatedField now accepts serializer methods which return querysets. Nov 4, 2015
@jsenecal
Copy link
Member

@rollokb Please answer to @jerel 's questions/comments or we will otherwise have to close this PR.

Thanks :)

@sonthonaxrk
Copy link
Contributor Author

@jerel sorry I just saw these in my email. I'll get back to you by the evening (UTC).

@jsenecal
Copy link
Member

@rollokb any news on this ?

@cpa
Copy link

cpa commented Mar 17, 2016

If the original author is fine with it, I'd like to take over this PR.

@jerel, which part of the PR are you referencing in this comment?

The way our ResourceRelatedField was implemented before wasn't ideal (source was ambiguous between serializer and model) so this functionality has been moved to the more explicitly named SerializerMethodResourceRelatedField since you opened this PR.

Thanks!

@jerel
Copy link
Member

jerel commented Mar 18, 2016

@cpa the functionality should ideally be moved into SerializerMethodResourceRelatedField with many=True instead of adding ManyResourceRelatedField like this PR does.

@jerel jerel closed this Mar 18, 2016
jerel added a commit that referenced this pull request Apr 12, 2016
jerel added a commit that referenced this pull request Apr 12, 2016
jerel added a commit that referenced this pull request Apr 12, 2016
jerel added a commit that referenced this pull request Apr 29, 2016
* docs: note about importing serializers

* Document ResourceRelatedField and RelationshipView

* Updated pip install instructions for 2.0.0-beta.2

* Add LimitOffsetPagination

* Dont let the offset go into negative space

* Add basic unit test for LimitOffsetPagination

* Support deeply nested includes
Allow skipping of intermediate included models

* Add current tox.ini directory to PYTHONPATH in order to use imports form there
Fix regression on PY3 caused by unicode_literals

* [FEATURE]: support using get_serializer_class on view

* fixed extract_root_meta for lists

* Fixed get_resource_name in case of non-model backed serializer.

Closes #219

* ResourceRelatedField now accepts serializer methods when many=True

* Rename "suggested" posts to "featured" so we can use suggested as many=True

* Updated SerializerMethodResourceRelatedField to allow many=True

Issue #151
Closes #220

* Correct error responses for projects with different DRF-configurations (#222)

* [#214] Add error messages tests.

* [#214] Extract formatting DRF errors.

* Add example view with custom handle_exception.

* Use HTTP 422 for validation error responses.

* Add full example of class-configured json api view.

* Fixed naming that suggested settings were used to inflect relationship names.

JSON_API_FORMAT_RELATION_NAME actually inflected the `type` instead.
The relation name is not changable at this time although if it woudl
be useful to someone it would be fine to implement it.
Closes #136.

* Updated changelog

* Added a doc note to prefer setting resource_name on serializers or models.

Closes #207

* Added get_related_field_name method to RelationshipView

* Added get_related_field_name method to RelationshipView

* Added docs about field_name_mapping

* Updated the readme for testing (#234)

* Allow exception handler to be used by normal DRF views: (#233)

* Add top-level 'errors' object to non-JSON-API responses
* Allow configuring the exception handler to be used _only_
  in JSON API views or uniformly across all views

* Fix included resource type inconsistency (#229)

When setting `resource_name = None`, the related instance's resource
name is used in `relationships`, but `None` is used in `included`.

This is related to #94 and #124

* Fixes #230. Keep write only fields from having an attribute key

* Release v2.0.0

* Update setup.py to classify as production/stable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants