Not really sure how I can't figure this out but I am trying to POST a form to alter the contents of get_queryset() but it isn't keeping the filtered items, it calls get_queryset multiple times during a POST for a form and loses the POST filtering as it calls GET too. I must be doing something wrong as this should be easy. I can only seem to alter the queryset with GET variables, how do I port this to work with post, can someone please help point me somewhere?
I'm using GET variables to alter the filtering now (which is somewhat fine with edits I made earlier to shorten the string), but I need to use POST for better efficiency as my form posts over 400 items in a multiple choice field so the URL gets really long and has chances for browser failure, and causing HTTP 414 Errors due to the data URL being too long.
This is my example trying to filter with POST
class GeneralLedgerDatatableView(DatatableView):
model = GeneralLedger
datatable_class = GeneralLedgerDatatable
template_name = 'accounting/general_ledger/general_ledger_list.html'
def post(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
context = self.get_context_data()
return self.render_to_response(context)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
title = 'General Ledger: Transaction Detail'
context['title'] = title
properties = Property.objects.filter(active=True).order_by('name')
accounts = GLAccount.objects.all().order_by('sortable_account_identifier')
current_property = self.request.session.get('property')
if current_property:
current_property = current_property.pk
form = SelectFilterOptionsForm(self.request.POST or None, properties=properties, accounts=accounts, initial={
'from_date': timezone.now().replace(day=1, month=1).strftime('%m/%d%/%Y'),
'accounts': [x.pk for x in accounts],
'to_date': timezone.now().strftime('%m/%d%/%Y'),
'property': current_property
})
context['form'] = form
return context
def get_queryset(self):
queryset = super().get_queryset()
if self.request.POST:
# THIS IS CALLED BUT SO IS GET AND ALL ARE RETURNED?
property = self.request.POST.get('property')
accounts = [x for x in self.request.POST.getlist('accounts') if x]
from_date = self.request.POST.get('from_date')
to_date = self.request.POST.get('to_date')
q_items = list()
if property:
# filter_dict['journal_line_item__property__pk__in'] = properties
q_items.append(Q(
property=property,
))
q_items.append(Q(
account__pk__in=accounts,
))
q_items.append(
Q(
date_entered__date__gte=datetime.datetime.strptime(from_date, '%m/%d/%Y'),
date_entered__date__lte=datetime.datetime.strptime(to_date, '%m/%d/%Y'),
)
)
queryset = queryset.select_related(
'transaction_code',
'book',
'account',
'property'
).filter(*q_items)
return queryset