You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
return', '.join([str(genre) for genre in album.genres.all()])
86
-
86
+
87
87
classMeta:
88
88
model = Album
89
89
fields = (
@@ -98,29 +98,29 @@ albums/views.py:
98
98
from rest_framework import viewsets
99
99
from .models import Album
100
100
from .serializers import AlbumSerializer
101
-
102
-
101
+
102
+
103
103
defindex(request):
104
104
return render(request, 'albums/albums.html')
105
-
106
-
105
+
106
+
107
107
classAlbumViewSet(viewsets.ModelViewSet):
108
108
queryset = Album.objects.all().order_by('rank')
109
109
serializer_class = AlbumSerializer
110
110
111
111
urls.py:
112
112
113
113
.. code:: python
114
-
114
+
115
115
from django.conf.urls import url, include
116
116
from rest_framework import routers
117
117
from albums import views
118
-
119
-
118
+
119
+
120
120
router = routers.DefaultRouter()
121
121
router.register(r'albums', views.AlbumViewSet)
122
-
123
-
122
+
123
+
124
124
urlpatterns = [
125
125
url('^api/', include(router.urls)),
126
126
url('', views.index, name='albums')
@@ -229,7 +229,7 @@ In the above example, the 'get_options' method will be called to populate the re
229
229
.. important::
230
230
231
231
To sum up, **the most important things** to remember here are:
232
-
232
+
233
233
- don't forget to add ``?format=datatables`` to your API URL
234
234
- you must add a **data-data attribute** or specify the column data property via JS for each columns, the name must **match one of the fields of your DRF serializers**.
235
235
@@ -334,7 +334,7 @@ We could also have written that in a more conventional form (without data attrib
334
334
{'data':'year'},
335
335
{'data':'genres', 'name':'genres.name'},
336
336
]
337
-
337
+
338
338
});
339
339
});
340
340
</script>
@@ -372,3 +372,24 @@ If you need more complex filtering and ordering, you can always implement your o
372
372
373
373
374
374
You can see this code live by running the :doc:`example app <example-app>`.
375
+
376
+
377
+
Handling Duplicates in Sorting
378
+
------------------------------
379
+
If sorting is done on a single column with more duplicates than the page size it's possible than some rows are never retrieved as we traverse through our datatable. This is because of how order by together with limit and offset works in the database.
380
+
381
+
As a workaround for this problem we add a second column to sort by in the case of ties.
382
+
383
+
class AlbumViewSet(viewsets.ModelViewSet):
384
+
queryset = Album.objects.all().order_by('year')
385
+
serializer_class = AlbumSerializer
386
+
datatables_additional_order_by = 'rank'
387
+
388
+
def get_options(self):
389
+
return "options", {
390
+
"artist": [{'label': obj.name, 'value': obj.pk} for obj in Artist.objects.all()],
391
+
"genre": [{'label': obj.name, 'value': obj.pk} for obj in Genre.objects.all()]
0 commit comments