Django json api ordering_fields not working on include resources
Hi I am working on ordering_fields on include resources using django_json_api. I have 2 models activities and activity_enrollments(activity is foreign_key). I am ordering on 2 fields one is related to activity.priority and next is activity_enrollment.is_targeted. is_targeted field order is working fine but activity__priority is not working
I have tried multiple columns in activity_enrollment model these are i am able to sort it. but related fields are not working
viewset
from rest_framework_json_api import filters
from rest_framework_json_api import django_filters
from rest_framework.filters import SearchFilter
from rest_framework import status, viewsets
from .models import ActivityEnrollment, Activity
from .serializers import ActivityEnrollmentSerializers
class EnrollmentsViewSet(viewsets.ModelViewSet):
"""
A simple ViewSet for viewing and editing accounts.
"""
queryset = ActivityEnrollment.objects.all()
serializer_class = ActivityEnrollmentSerializers
filter_backends = (filters.QueryParameterValidationFilter, filters.OrderingFilter,
django_filters.DjangoFilterBackend, SearchFilter)
ordering_fields = ("activity__priority","is_targeted",)
serializers
from .models import ActivityEnrollment, Activity
from rest_framework_json_api.serializers import ModelSerializer
class ActivitySerializers(ModelSerializer):
class Meta:
model = Activity
fields = (
'id',
'name',
'priority',
)
class ActivityEnrollmentSerializers(ModelSerializer):
included_serializers = {
"activity": ActivitySerializers
}
class Meta:
model = ActivityEnrollment
fields = (
"id",
"is_targeted",
"completed_at",
"activity"
)
class JSONAPIMeta:
included_serializers = ['activity']
api:
GET /api/activity-enrollment/?include=activity&sort=-activity__priority
response
{
"links": {
"first": "http://127.0.0.1:8908/api/activity-enrollment/?include=activity&page%5Bnumber%5D=1&sort=-activity__priority",
"last": "http://127.0.0.1:8908/api/activity-enrollment/?include=activity&page%5Bnumber%5D=1&sort=-activity__priority",
"next": null,
"prev": null
},
"data": [
{
"type": "ActivityEnrollment",
"id": "3",
"attributes": {
"is_targeted": false,
"completed_at": "2025-01-06T17:08:00Z"
},
"relationships": {
"activity": {
"data": {
"type": "Activity",
"id": "3"
}
}
}
},
{
"type": "ActivityEnrollment",
"id": "2",
"attributes": {
"is_targeted": true,
"completed_at": "2025-01-14T17:08:00Z"
},
"relationships": {
"activity": {
"data": {
"type": "Activity",
"id": "62"
}
}
}
},
{
"type": "ActivityEnrollment",
"id": "1",
"attributes": {
"is_targeted": false,
"completed_at": null
},
"relationships": {
"activity": {
"data": {
"type": "Activity",
"id": "1"
}
}
}
}
],
"included": [
{
"type": "Activity",
"id": "1",
"attributes": {
"name": "test1",
"priority": 1
}
},
{
"type": "Activity",
"id": "2",
"attributes": {
"name": "test2",
"priority": 62
}
},
{
"type": "Activity",
"id": "3",
"attributes": {
"name": "test3",
"priority": 3
}
}
],
"meta": {
"pagination": {
"page": 1,
"pages": 1,
"count": 3
}
}
}