Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How can I paginate included resources?#969

Answeredbysliverc
esmail asked this question inQ&A
Discussion options

Per the spec., this is supposed to be possible:

...To paginate an included collection returned in a compound document, supply pagination links in the corresponding links object.
https://jsonapi.org/format/#fetching-pagination

But they don't mention a queryparam I could try out, and I didn't notice any mention of included resource pagination in the docs. for this project.

You must be logged in to vote

It is currently only possible to dopagination on top level resources in DJA but not the included collection.

The specification states any pagination to be optional so if we wanted this to be added as a feature in DJA we should discuss actual use cases which such pagination would cover and then how to technically implement it.

A server MAY choose to limit the number of resources returned in a response to a subset (“page”) of the whole set available.

My first thought is that such a pagination is fairly limited as included collections can be a collection of different types. I haven't tested this but you could have related urls links which then allow you to paginate. This way your client c…

Replies: 1 comment 3 replies

Comment options

It is currently only possible to dopagination on top level resources in DJA but not the included collection.

The specification states any pagination to be optional so if we wanted this to be added as a feature in DJA we should discuss actual use cases which such pagination would cover and then how to technically implement it.

A server MAY choose to limit the number of resources returned in a response to a subset (“page”) of the whole set available.

My first thought is that such a pagination is fairly limited as included collections can be a collection of different types. I haven't tested this but you could have related urls links which then allow you to paginate. This way your client can choose specifically what relationship needs to be paginated.

Just some thoughts. It would be great to hear what you actual use case is for such a feature and whether the above would cover it.

You must be logged in to vote
3 replies
@esmail
Comment options

Thanks for the response. A simplified version of my use case that should still capture the important aspects is an endpoint providing grade data for per school semester in which more student-level data is available than the consuming client can receive at once. The criteria in their current form are along the lines of the following:

  1. The object types in question areSemester (desired top level),StudentGroup (included), andStudent (nested include with pagination desired)
  2. The relationship betweenSemester andStudentGroup is one to many (student groups are particular to a semester), and the relationship betweenStudentGroup andStudent is many to many. Consequently, the indirect relationship betweenSemester andStudent (throughStudentGroup) is also many to many (e.g. a student might've been a part of multiple semesters
  3. A list/detail endpoint pair is wanted forSemester
  4. The detail endpoint is meant to show aggregate data at the semester level (e.g. the average numerical grade across all students in the semester), aggregate data at the student group level, and individual grade data at the student level
  5. The individual student grade data is specific to the context of the given semester (e.g. their grade average for the semester)
  6. The consuming client is limited in how much data it can take in at once, so pagination at the student level is desired
@esmail
Comment options

I'm curious to hear any thoughts on how this might be implemented in a custom pagination class (if possible) or otherwise.

@sliverc
Comment options

Sorry for my late response. Finally have some time to respond.

Thanks for your example. Can you elaborate what the exact limitation the client has and why? To have proper pagination you would need a predictable order. The issue is that in JSON:API spec an included item must not be added twice. So if you paginate through the groups and the students at some point a student would simply be missing as already retrieved in an earlier page.

In your specific mentioned case I would design the API the way that the client gets semester with semester group included. Then it calls student with a filter of all student groups retrieved and then can easily paginate through the students. Just a thought to think about (maybe you already do it this way).

In terms of custom pagination class. Pagination class is a DRF feature and only has access to the main resources. The included resources are only extracted in therest_framework_json_api.renderers.JSONRenderer class. You could make a custom renderer and overwrite the methodextract_included.

Answer selected byesmail
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@esmail@sliverc

[8]ページ先頭

©2009-2025 Movatter.jp