How to add an initial/default value using Django Filters?

You can try overriding the __init__ method of UserFilter:

def __init__(self, *args, **kwargs):
    super(UserFilter, self).__init__(*args, **kwargs)
    self.form.initial['archive'] = False

For DRF you can try override __init__:

def __init__(self, *args, **kwargs):
    kwargs['data']._mutable = True
    if 'archive' not in kwargs['data']:
        kwargs['data']['archive'] = False
    kwargs['data']._mutable = False
    super(UserFilter, self).__init__(*args, **kwargs)

But you should read django-filter.readthedocs.io...using-initial-values-as-defaults

Tags:

Python

Django