django-filter

Django SimpleListFilter: ValueError not enough values to unpack (expected 2, got 0)

Django SimpleListFilter: ValueError not enough values to unpack (expected 2, got 0) Question: Context: I’m creating a custom input filter in Django using the SimpleListFilter class. I’ve followed the steps in here https://hakibenita.com/how-to-add-a-text-filter-to-django-admin, but I’m encountering a ValueError when trying to use the filter. Here is my code: filters.py: from django.contrib import admin from django.db.models …

Total answers: 1

django_filters returns True is not a valid field when filtering through rest api

django_filters returns True is not a valid field when filtering through rest api Question: Got in my model serializer such field like is_favorited and others is_favorited = serializers.SerializerMethodField() def get_is_favorited(self, obj): user = self.context[‘request’].user if user.is_anonymous: return False qs = Favorite.objects.filter(user=user, recipe=obj) return len(qs) > 0 my model viewset contains queryset = Recipe.objects.all() filter_backends = …

Total answers: 1

django-filter how to add attribute to html tag under form

django-filter how to add attribute to html tag under form Question: I’m using Django-filter,I tried to add attributes by using widget and attrs: filter.py: class MyFilter(django_filters.FilterSet): messageText = django_filters.CharFilter(widget=(attrs={‘style’:’width: 20px’, ‘class’:’form-select form-select-sm’})) class Meta: model = Mymodel fields = [‘messageText ‘] Then I got: SyntaxError: invalid syntax at here ‘attrs={‘ Any friend can help ? …

Total answers: 1

Django Filter by ForeignKey value

Django Filter by ForeignKey value Question: Suppose I have two models class DocumentType(BaseModel): name = models.CharField(max_length=128) code = models.CharField(max_length=128) exp_days = models.PositiveIntegerField("Remind me before (Days)", blank=True, null=True) def __str__(self): return str(self.name) class Document(BaseModel): type = models.ForeignKey("masters.DocumentType", on_delete=models.PROTECT) date_of_expiry = models.DateField(blank=True, null=True) attachment = models.FileField(blank=True, null=True) def __str__(self): return str(self.employee) How can I retrieve documents with …

Total answers: 1

Django: Perform GROUP BY over a view queryset

Django: Perform GROUP BY over a view queryset Question: I need to perform group by over a queryset of objects already filtered by standard django filters in order to filter by how many objects in queryset are related to same foreign key object. My code now (does not work): class CustomerTicketsViewSet(AbstractViewSet): queryset = CustomerTicket.objects.all() serializer_class …

Total answers: 1

3 django model filterset with django-filter

3 django model filterset with django-filter Question: I have 3 different django models and I want to filter using django-filter. The ORM query I wrote below works without any problems. but how can i do this with django-filter (filters.FilterSet) ? Employee.objects.filter(companyrecord__company__cname__icontains="ompanyname") THIS IS WORKS it is work for me in ORM. But how can i …

Total answers: 1

filter price range by django-filter

filter price range by django-filter Question: I added a price range filter by Django-filter but seems it doesn’t work. filters.py from django_filters import FilterSet from .models import Apartment class ApartmentFilter(FilterSet): class Meta: model = Apartment fields = { ‘price’: [‘lt’,’gt’] } views.py class ApartmentViewSet(viewsets.ModelViewSet): queryset = Apartment.objects.all().order_by(‘-timestamp’) serializer_class = ApartmentSerializer permission_classes = [ permissions.IsAuthenticatedOrReadOnly, IsOwnerApartmentOrReadOnly] …

Total answers: 2

Django-filter error: 'Meta.fields' must not contain non-model field names

Django-filter error: 'Meta.fields' must not contain non-model field names Question: I am working with Django REST framework and django-filters and and I’d like to use the reverse relationship annotation_set as one of filters for a GET API that uses the model Detection. The models are the following: class Detection(models.Model): image = models.ImageField(upload_to="detections/images") def local_image_path(self): return …

Total answers: 3

Django – How to add "or" condition to queryset.filter in custom filter

Django – How to add "or" condition to queryset.filter in custom filter Question: I want to make a search filter which searches in multiple fields with multiple conditions, using only one search field. I have this filters.py file: import django_filters from .models import Product class ProductFilter(django_filters.FilterSet): q = django_filters.CharFilter(method=’search_filter’, label=’Cerca’) class Meta: model = Product …

Total answers: 2

Django: Django_filters in a class-based views

Django: Django_filters in a class-based views Question: I ask you if you know how put this filters : class CoursesFilters(django_filters.FilterSet): class Meta: model = Courses exclude = (‘description’) in this class view : class CoursesList(ListView): model = Courses template_name = ‘courses_list.html’ I used to build my applications using function-based views, and this is my first …

Total answers: 2