10👍
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
8👍
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
- Resolving AmbiguousTimeError from Django's make_aware
- Django admin inline: select_related
- Is there a Django template tag that lets me set a context variable?
1👍
I had a similar problem in that I wanted to preserve the users selections even after they had navigated away from the page. I implemented my solution in the Filter View as follows…
class BucketLookupView(UserPassesTestMixin,SingleTableMixin,FilterView):
template_name = "finance/planning-bucket-lookup.html"
model = finance_models.SalesOrderBucket
filterset_class = finance_filters.BucketFilter
table_class = finance_tables.BucketTable
def get_filterset_kwargs(self,*args):
kwargs = super().get_filterset_kwargs(*args)
if kwargs['data']:
bucket_filter_data = kwargs['data']
self.request.session['bucket_filter_data']= bucket_filter_data
else:
if 'bucket_filter_data' in self.request.session.keys():
kwargs['data']=self.request.session['bucket_filter_data']
return kwargs
0👍
Here is an approximate code snippet I used:
def list(self, request, *args, **kwargs):
filters = QueryDict('is_archive=true', mutable=True)
filters.update(request.GET)
You build a new QueryDict as request.GET is immutable. Not that efficient, but looks pretty to me.
- Django.db.utils.OperationalError: near "[]": syntax error
- Can I have some code constantly run inside Django like a daemon
- How to get Interdependent dropdowns in django using Modelform and jquery?
0👍
Here is a remix of @IlyaPetukhov’s answer that I find easier to read:
def __init__(self, data=None, *args, **kwargs):
if data is not None:
data = data.copy()
data.setdefault("archive", False)
super(UserFilter, self).__init__(data, *args, **kwargs)
If you’re uncomfortable with data = data.copy()
, you can replace it with data._mutable = True
; just know that it’s not officially supported by Django.
- Django 1.3: Outbox empty during tests
- How to add some extra fields to the page in django-cms? (in django admin panel)
- Python classes that refer to each other
- Django: I get a [relation "auth_group" does not exist] error after syncdb
- Psycopg2 cannot connect to docker image
0👍
form.cleaned_data is using to add filters to the queryset and no initial value is set for cleaned data.
The solution could be to override form property:
@property
def form(self) -> ModelForm:
form = super().form
form.data = form.data.copy()
form.data.setdefault("archive", False)
return form
0👍
After reading django-filter warnings, I opted to redirect if filter is not set:
from django.http import QueryDict
class EntrepriseList(LoginRequiredMixin, SingleTableMixin, FilterView):
table_class = EntrepriseTable
model = Entreprise
template_name = 'stage/entreprise_list.html'
filterset_class = EntrepriseFilter
paginate_by = 30
def get(self, request, *args, **kwargs):
if request.GET:
return super().get(request, *args, **kwargs)
else:
qd = QueryDict(mutable=True)
qd['archive'] = False
urlnext = reverse('current_page_name') # or just url from request
urlnext += f'?{qd.urlencode()}'
return HttpResponseRedirect(urlnext)
- Django Rest Framework reverse and SimpleRouter
- Django admin foreign key drop down field list only "test object"
- Django unable to load test fixtures, IntegrityError
- Make browser submit additional HTTP-Header if click on hyperlink