Comments (3)
Hi,
If the JSON returned is correct, chances are your problem is on the client side code. Maybe you're not building your Datatable correctly. Good luck.
from django-rest-framework-datatables.
I'm seeing the problem in both the provided example and in my own project. Before using distinct everything works, but when applying distinct things go awry.
By playing around with things I did find that the reason rank works, but year doesn't is that rank is the first column called in the table. If I filter by distinct('year') and then set columns in the DataTable like this:
"columns": [
{"data": "year"},
{"data": "rank", "searchable": false},
// Use dot notation to reference nested serializers.
// This data: could alternatively be displayed with the serializer's ReadOnlyField as well, as seen in the minimal example.
{"data": "artist.name", "name": "artist.name"},
{"data": "name"},
{"data": "genres", "name": "genres.name", "sortable": false},
],
then I get a valid DataTable. Whatever field is distinct must be the first field called in the table. The I can use ColReorder to move the columns around as needed.
The problem now is that while I get a table now, I can ONLY sort by the column that is distinct. Attempting to sort by any other column (using the table headers) returns ajax errors, both in the (modified) example project and in my own project.
from django-rest-framework-datatables.
I seem to have found a solution, though I don't know why it works.
If I change:
class AlbumViewSet(viewsets.ModelViewSet):
queryset = Album.objects.all().order_by('year', 'rank').distinct('year')
serializer_class = AlbumSerializer
to:
class AlbumViewSet(viewsets.ModelViewSet):
album = Album.objects.all().order_by('year').distinct('year')
queryset = Album.objects.filter(id__in=album)
serializer_class = AlbumSerializer
... all works perfectly! Sorting, filtering, etc all work now. An extra query is required, but it's worth it. Still not sure why simply applying distinct fails to work properly, but I can live with this modification
from django-rest-framework-datatables.
Related Issues (20)
- "Illegal mix of collations for operation 'like'" utf8 search error HOT 3
- Travis build not running HOT 1
- DatatablesPageNumberPagination Ignores "PAGE_SIZE" default parameter in settings.py
- How to filter by date HOT 1
- DatatablesFilterBackend: performance gain by removing call to count() HOT 3
- Serverside sorting with ForeignKey produces duplicate HOT 4
- Export all the results to the CSV HOT 1
- DatabaseError when using search HOT 1
- Issue retrieving fields with a 'source' attribute HOT 2
- Can't get filter method to work
- filter post with id
- can not disable paginintion for specific views HOT 2
- d.date and d.images return undefined in ?format=datatables but not with ?format=json HOT 1
- Sorting and pagintion and searching produces problemes
- Fail when using SQL query 'RawQuerySet' object has no attribute 'count' HOT 3
- deprecation warning due to no pyproject.toml
- Project dead? HOT 1
- Support for Datatables SearchBuilder?
- Default global and column search is not working in POST method
- Datatables incorrectly calculates the number of pages with custom filter_backend
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-rest-framework-datatables.