question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Django-Filter Quickstart Documentation: Subclassing of DatatablesFilterSet

See original GitHub issue

First, thanks to the folks who work on this package I really needed it today.

To those trying to implement the Django-filter subpackage and finding it missing, it is not in the current release, but it is in master. See #82

Once you have that, and are following the Django-Filter Quickstart, you might run into some confusion on what gets subclassed.

From my experience trying to repeat the docs, this line:

class AlbumGlobalFilter(GlobalFilter):

triggers the following error:

AttributeError: type object 'AlbumGlobalFilter' has no attribute '_meta'

I believe instead AlbumGlobalFilter should subclass DatatablesFilterSet, which is imported in the example but never implemented. i.e.:

class AlbumGlobalFilter(DatatablesFilterSet):

Which makes some sense given the idea of using the DataTables version of various imports otherwise handled by django-filter i.e. filters.FilterSet.

This may be correct for the example in the docs, but I was also stumped up by the Meta class in the current docs. The fields = '__all__' resulted in an error, I believe because I did not declare all of the fields from my model:

TypeError: 'Meta.fields' contains fields that are not defined on this FilterSet: __all__

I was able to get django-rest-framework-datatables and the django-filter to ultimately work for my subset of model fields. In case someone else is might appreciate an example, it is simply something like:

Declare the field-level global filter

class GlobalCharFilter(GlobalFilter, filters.CharFilter):
    pass

Then declare the Filterset with only the necessary fields:

class FacilityFilterCrud(DatatablesFilterSet):
    name = GlobalCharFilter(lookup_expr='icontains')

    class Meta:
        model = Facility
        fields = ['__name__']

A partial example like this in the docs, might be helpful to help demonstrate some of the more fine-grained control hinted at in the introduction.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
izimobilcommented, Nov 8, 2021
1reaction
banagalecommented, Apr 21, 2021

@izimobil Sure. I typically look for a go-ahead from a maintainer before investing the time, which was why I created the issue.

I have another outstanding docs PR request in scrapy. I’ll try and handle them both at the same time.

Feel free to assign this to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Integration with Django-filter
Integration with Django-filter is provided through Datatables-specific DatatablesFilterSet and DatatablesFilterBackend classes.
Read more >
django-tables2
Support for automatic table generation based on a Django model. • Supports custom column functionality via subclassing. • Pagination.
Read more >
django-rest-framework-datatables
Seamless integration between Django REST framework and Datatables. ... Django-Filter Quickstart Documentation: Subclassing of DatatablesFilterSet $ 0.
Read more >
Pagination - Django REST framework
To create a custom pagination serializer class, you should inherit the subclass pagination.BasePagination , override the paginate_queryset(self, queryset, ...
Read more >
CRUD
Model Create/Read/Update/Delete/List viewset. Lightweight alternative for django admin. CRUD interface based in the django generic views. Viewset provides a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found