GithubHelp home page GithubHelp logo

Comments (3)

izimobil avatar izimobil commented on May 18, 2024

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.

JackAtOmenApps avatar JackAtOmenApps commented on May 18, 2024

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.

JackAtOmenApps avatar JackAtOmenApps commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.