Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

LTE and GTE filters do not work inside Viewset

I defined DjangoFilterBackend inside filter_backends and added (tried to) the correct definitions as shown in filter_fields. It does not give any error in url search but it does not give any result (as filtered) in shown URL:

http://127.0.0.1:8000/api/publicusers/?deliveredtotime__lte=50
class User(AbstractUser, PermissionsMixin):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    username = models.CharField(max_length=40, unique=True, default='undefinedusername')
    deliveredtotime = models.IntegerField(default='0')

class PublicUserViewSet(viewsets.ModelViewSet):
    permission_classes = (IsAuthenticated,)
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_backends = [filters.SearchFilter,DjangoFilterBackend]
    pagination_class = StandardResultsSetPagination
    filter_fields = { 'deliveredtotime': ['gte', 'lte']  }

What am I doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

filter_fields has been renamed to filterset_fields since :

class PublicUserViewSet(viewsets.ModelViewSet):
    permission_classes = (IsAuthenticated,)
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_backends = [filters.SearchFilter, DjangoFilterBackend]
    pagination_class = StandardResultsSetPagination
    filterset_fields = {
        'deliveredtotime': ['gte', 'lte']
    }  # 🖘 not filter_fields
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading