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
class F(FilterSet):
"""Filter for Books by if books are published or not"""
published = BooleanFilter(field_name='published_on', method='filter_published')
def filter_published(self, queryset, name, value):
# construct the full lookup expression.
lookup = '__'.join([name, 'isnull'])
return queryset.filter(**{lookup: False})
# alternatively, you could opt to hardcode the lookup. e.g.,
# return queryset.filter(published_on__isnull=False)
class Meta:
model = Book
fields = ['published']
# Callables may also be defined out of the class scope.
def filter_not_empty(queryset, name, value):
lookup = '__'.join([name, 'isnull'])
return queryset.filter(**{lookup: False})
class F(FilterSet):
"""Filter for Books by if books are published or not"""
published = BooleanFilter(field_name='published_on', method=filter_not_empty)
class Meta:
model = Book
fields = ['published']
I think it would be better to change it as following.
If "published" field is present in "Book" model, then
# published = BooleanFilter(field_name='published_on', method='filter_published')
published = BooleanFilter(field_name='published', method='filter_published')
If "published_on" field is present in "Book" model, then
class Meta:
model = Book
# fields = ['published']
fields = ['published_on']
I've made pull requests #1441, #1442 for each pattern.
Reason
I think field_name in BooleanFilter should match the field name in the model.
# models.py
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=255)
class Book(models.Model):
name = models.CharField(max_length=255)
published = models.DateTimeField(null=True)
# serializers.py
from rest_framework import serializers
from .models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = "__all__"
# views.py
from django_filters import BooleanFilter
from django_filters import rest_framework as filters
from rest_framework import generics
from .serializers import BookSerializer
from .models import Book
class F(filters.FilterSet):
published = BooleanFilter(field_name="published_on", method="filter_published")
def filter_published(self, queryset, name, avlue):
lookup = "__".join([name, "isnull"])
print(lookup)
return queryset.filter(**{lookup: False})
class Meta:
model = Book
fields = ["published"]
class BookListAPIView(generics.ListAPIView):
serializer_class = BookSerializer
queryset = Book.objects.all()
filter_backends = [filters.DjangoFilterBackend]
filterset_class = F
For the following codes in
https://django-filter.readthedocs.io/en/stable/ref/filters.html#method
I think it would be better to change it as following.
I've made pull requests #1441, #1442 for each pattern.
Reason
I think field_name in BooleanFilter should match the field name in the model.
Test
I used the code in
https://django-filter.readthedocs.io/en/stable/ref/filters.html#method
as a reference and tested it.
The test resulted in the following error.
The test was successful when I modified it as pull requests #1441, #1442.
Adopting pull requests #1441 or #1442 is recommending.
The text was updated successfully, but these errors were encountered: