Skip to content

include usage #318

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
morenoh149 opened this issue Jan 26, 2017 · 4 comments
Closed

include usage #318

morenoh149 opened this issue Jan 26, 2017 · 4 comments

Comments

@morenoh149
Copy link
Contributor

I'm having difficulty understanding how to use ?include=.... Say I have drfja properly installed, do you simply write your viewset code and include query params are available?

Or do you have to manually call http://django-rest-framework-json-api.readthedocs.io/en/stable/api.html?highlight=include#extract-included I'm confused.

@abdulhaq-e
Copy link
Contributor

Hello, there is nothing special about views, it's the serialiser that you must customise:

To allow querying with ?include=profile:

class UserSerialiser(serializers.ModelSerializer):

    included_serializers = {
        'profile': 'Gienah.administration.serialisers.FacultySerialiser',
    }

    class Meta:
        model = User
        fields = ('name', 'profile')

To include profile by default:

class UserSerialiser(serializers.ModelSerializer):

    included_serializers = {
        'profile': 'Gienah.administration.serialisers.FacultySerialiser',
    }

    class Meta:
        model = User
        fields = ('name', 'profile')

    class JSONAPIMeta:
        included_resources = ['profile']

@morenoh149
Copy link
Contributor Author

morenoh149 commented Feb 2, 2017

okay. Say I have a viewset and a serializer. The viewset has a method activity. I attempted /resource/:id/activity?include=foo and did not receive the related foo.

#  views.py
class ResourceViewSet():
  def activity():
    return rest_framework.response.Response(...)
# serializers.py
class ResourceHyperlinkedSerializer():
  included_serializers = {
    'foo': '...'
  }
  class Meta:
    model = Resource
    fields = ('foo')

However if I do /resource/:id?include=foo the related foo is properly returned in the included array. What would be missing here?

@sliverc
Copy link
Member

sliverc commented May 22, 2018

I think to be able to assist you better could you expand on how your ResourceViewSet looks like?

Maybe create a gist with the full implementation of the view if possible. Most likely the problem is hidden there somewhere...

@morenoh149
Copy link
Contributor Author

I've lost the context I had when I asked this and no longer have access to that codebase. Closing.

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

No branches or pull requests

3 participants