{"library":"django-filter","code":"import django_filters\nfrom django.db import models\nfrom django.views.generic import ListView\nfrom rest_framework import generics\nfrom rest_framework import serializers\nfrom django_filters.rest_framework import DjangoFilterBackend\n\n# 1. Define a Django Model\nclass Product(models.Model):\n    name = models.CharField(max_length=255)\n    price = models.DecimalField(max_digits=5, decimal_places=2)\n    release_date = models.DateField(null=True, blank=True)\n    in_stock = models.BooleanField(default=True)\n\n    def __str__(self):\n        return self.name\n\n    class Meta:\n        app_label = 'myapp' # Required for models in standalone snippets\n\n# 2. Define a FilterSet for the Model\nclass ProductFilter(django_filters.FilterSet):\n    name = django_filters.CharFilter(lookup_expr='icontains')\n    price_gt = django_filters.NumberFilter(field_name='price', lookup_expr='gt')\n    price_lt = django_filters.NumberFilter(field_name='price', lookup_expr='lt')\n\n    class Meta:\n        model = Product\n        fields = ['name', 'price', 'release_date', 'in_stock']\n\n# 3. Use with a Django Class-Based View\n# (Requires Django URL configuration and template setup)\nclass ProductListView(ListView):\n    model = Product\n    template_name = 'product_list.html' # Dummy template for example\n    context_object_name = 'products'\n\n    def get_queryset(self):\n        queryset = super().get_queryset()\n        filter = ProductFilter(self.request.GET, queryset=queryset)\n        return filter.qs\n\n    def get_context_data(self, **kwargs):\n        context = super().get_context_data(**kwargs)\n        context['filter'] = ProductFilter(self.request.GET, queryset=self.get_queryset())\n        return context\n\n# 4. Use with Django REST Framework\nclass ProductSerializer(serializers.ModelSerializer):\n    class Meta:\n        model = Product\n        fields = '__all__'\n\nclass ProductAPIView(generics.ListAPIView):\n    queryset = Product.objects.all()\n    serializer_class = ProductSerializer\n    filter_backends = [DjangoFilterBackend]\n    filterset_class = ProductFilter # Or filterset_fields = ['name', 'price'] for simpler cases\n\n# To run this code, you would need:\n# - A Django project and app ('myapp').\n# - Add 'django_filters' and 'rest_framework' to INSTALLED_APPS in settings.py.\n# - Define URL patterns for ProductListView and ProductAPIView.\n# - Run migrations to create the Product model.\n# - Optionally, create 'product_list.html' template for the ListView.","lang":"python","description":"This quickstart demonstrates defining a `FilterSet` for a Django model and integrating it with a standard Django class-based view (`ListView`) and a Django REST Framework `generics.ListAPIView`. For DRF, `DjangoFilterBackend` is added to `filter_backends` and the `filterset_class` or `filterset_fields` is specified on the view. Remember to add `django_filters` and `rest_framework` to `INSTALLED_APPS` in your Django `settings.py`.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}