$ show carltongibson/django-filter
django-filter is a reusable Django application for dynamic QuerySet filtering from URL parameters. It provides declarative filter classes similar to Django's ModelForm, automatically generating filters from model fields with customizable lookup expressions. The library integrates with Django REST Framework via specialized FilterSet classes and filter backends, enabling filtering in both traditional Django views and DRF APIs. The architecture centers around FilterSet classes that process GET parameters to filter querysets, with supporting components for custom filters, widgets, and form fields. Key components include the FilterSet metaclass for automatic filter generation, a comprehensive library of filter types (CharFilter, BooleanFilter, RangeFilter, etc.), custom widgets for specialized input handling (BooleanWidget, LinkWidget, RangeWidget), and DRF integration backends. The system handles complex filtering scenarios including related fields, lookups, ordering, and CSV inputs. Internationalization support is built-in with translations for over 20 languages. The codebase follows Django conventions with clear separation of concerns between filter logic, presentation, and framework integration. The project demonstrates strong maintenance practices with active commits, comprehensive testing across multiple Django/Python versions, and clear documentation. It maintains compatibility with recent Django versions (5.2-6.1) and Python 3.10-3.14 while managing optional dependencies like djangorestframework and django-crispy-forms. The code shows careful attention to edge cases like null values, empty choices, and crispy forms integration, with deprecation paths for backward compatibility.
The LLM identified 4 files worth highlighting for reuse:
Contains the FilterSet class and FilterSetMetaclass that automatically generate filters from model fields. Implements declarative filter definition similar to ModelForm, handles field overrides, unknown field behaviors, and filter inheritance. Self-contained core component that could be extracted to another Django project with minimal changes.
Defines reusable filter classes (CharFilter, BooleanFilter, ChoiceFilter, RangeFilter, etc.) that encapsulate lookup logic and widget handling. Includes specialized filters for CSV inputs, ranges, and method-based filtering. Each filter is isolated and demonstrates non-obvious skill in handling edge cases like null booleans and empty choices.
Provides custom form widgets for filtering UI: BooleanWidget (triple-state handling), LinkWidget (hierarchical filtering), RangeWidget (date/numeric ranges), CSVWidget, and LookupChoiceWidget. Widgets are self-contained, solve specific presentation problems, and can be reused independently in Django forms.
Implements DjangoFilterBackend for Django REST Framework, adapting FilterSet to DRF's filtering interface. Handles get_filterset, filter_queryset, and HTML template rendering with crispy forms support. Demonstrates clean adapter pattern that integrates with DRF viewsets while maintaining separation of concerns.