Adding OrderingSchema for ordering QuerySets#1291
Adding OrderingSchema for ordering QuerySets#1291aryan-curiel wants to merge 25 commits intovitalik:masterfrom
Conversation
Added class OrderBaseSchema as an initial placeholder to allow using as endpoint query params the same way as FilterSchema
Verifies if the provided values are part of the allowed fields
It uses the provided order_by field value and order a provided queryset with it
|
How about |
Adding support for list type is definitely a good idea. However, not sure adding it to this sort method is the most convenient approach. |
|
May I ask if there is any progress on this OrderingSchema? I found same failure in these pull request actions. But I can not see these failed logs. What happened about this pull request? |
Honestly, I'm not sure. I haven't received any review, and find no sense in the failed tests related to schemas in the pipeline, so I haven't been able to fix it. If you need it with some level of urgency, feel free to bring the added code to your code base. |
|
For consideration on this topic, this library also exists and provides ordering support to django-ninja: https://eadwincode.github.io/django-ninja-extra/tutorial/ordering/ |
|
@crbunney Wow, thanks! Didn't know about it. I'm also doing a library for supporting filtering, ordering and pagination for Ninja and FastAPI with multiple data sources. I'm not a big fan of using decorators for this :( It feels like it;s obfuscating the logic. In any case, it's amazing that we have already something like ninja extras! |
|
Hi @aryan-curiel, just wanted to say a huge thank you for this PR! The OrderingSchema implementation is incredibly clean, elegant. It's exactly the solution I was looking for. Amazing work! |
|
I noticed that when order_by is empty ([]), calling Maybe we should only call |
@lthon-sha Thanks for that amazing feedback! I will update the PR according to your recommendation and add the needed tests |
The purpose of this PR is to not rewrite the default meta ordering by passing an empty list
|
@vitalik I would love a feedback from any of the more experienced contributors. It has been almost a year since the PR was opened, and I would love to move forward with it if it's something that you all consider it might bring some value. |
|
Sorry for dragging this for this long - but we finally getting rid of "Config" child class in favour of Meta (as it will be "broken" in future pedantic releases Could you rework Config -> Meta like django-ninja/ninja/filter_schema.py Lines 44 to 47 in 52d510a |
Considering the FilterSchema transitioned from using Config sucblass to sue Meta subclass, the OrderingBaseSchema class was updated following the same approach
|
Hi @vitalik That's awesome! Let me know if anything else is needed. |
|
Hi, @vitalik do you have any plan to merge this PR ? It seems like @aryan-curiel has made the change you asked ) |
|
This looks great, @aryan-curiel, thank you for putting this PR together! For the ordering param name, I think because this would be public to end users, we would want to have the query parameter itself be configurable rather than hardcoding to Similarly, it may also not always be desirable to expose direct model relationships in ordering to end users, so facilitating some type of mapping of declared filters to publicly facing options would be preferable. Unless I'm misunderstanding, this seems to basically just pass through to the ORM with some optional validation, but if we have a field with a lot of relationships or strange internal naming conventions, we would ideally be able to alias this for end users. For example, we could have a valid relationship |
|
@brendan-morin |
brendan-morin
left a comment
There was a problem hiding this comment.
This is a clean implementation of a popularly requested feature, looking forward to @vitalik's thoughts if this is good to merge.
| ```python | ||
| class BookOrderingSchema(OrderingSchema): | ||
| class Meta(OrderingSchema.Meta): | ||
| ordering_query_param = "ordering" |
| ```python hl_lines="3" | ||
| class BookOrderingSchema(OrderingSchema): | ||
| class Meta: | ||
| allowed_fields = { |
There was a problem hiding this comment.
Looks good, this offers some great flexibility. I think the duck-typing approach here to let this be either a dict or list is a good call and probably preferred to the alternate having two more strongly typed configs that could conflict (e.g. allowed_fields, allowed_fields_dict)
Problem
The
FilterSchemadefinition is a simple but effective approach to centralize logic around filtering QuerySets. A similar approach could be followed for ordering.Current for ordering and filtering in the same handler we would the following:
But what if we want to limit the API to allow ordering by just some of the fields. Or if we want to customize ordering based on custom fields and logic. What if we want to have a similar approach for other data sources, for example ElasticSearch.
Proposal
This PR propose to include a helper schema class, similar to FilteringSchema, but for ordering. It';s a simple schema class, with only one field:
order_by, that accepts a list of string.The allowed fields can be specified through the Config inner class, and a Pydantic validator will check that the provided query values are part of the allowed fields.
The schema then will provide a
.sort()method (similar to.filter()) that we can use to pass the query set, and expect it ordered as a returned value.The values can be provided using django standard behavior for descending order.
Example
Using it with out-of-the-box definition, allowing all fields from the model.
Using it with custom definition of allowed fields
Other ideas not followed
I also considered to have a default value field in the config, but decided to go with field default definition on custom schema level
Another consideration was to create a class method factory in the OrderingSchema, so it can be define inline, but I wasn't sure if it would be used:
Notes
I didn't add field validation with Model definition, to keep the practices followed in the FilterSchema definition.
Also, I think is a good idea to keep this helpers class as simple as possible, and give room to personalization for more complex scenarios. However, let me know if validating
allowed_fieldswith Model fields is something we would like to have, and I can update the PR.This was really useful in a personal project, were we needed to provide different ordering behaviors for a QuerySet and for an OpenSearch query. We had to do similar personalizations for pagination and filtering.