I am trying to query database on the basis of multiple parameters, basically I fetch all the records from a table and then filter them out according to the parameters passed. Although the search portion works fine but when the resultant query set is returned, I get the following error:
TypeError: Object of type date is not JSON serializable
I am not sure why that happens since I'm not trying to serialize anything.
My code looks something like:
view
class SearchView(LoginRequiredMixin, generic.ListView):
login_url = '/login/'
template_name = 'scand/search_result.html'
context_object_name = 'images'
def get_queryset(self):
form = SearchForm(self.request.GET)
query_dict = {}
if form.is_valid():
query_dict = form.cleaned_data
self.request.session['query_dict'] = query_dict
queryset = ImageTag.objects.search(query_dict)
print(queryset)
return queryset
model manager
class ImageTagManager(models.Manager):
def ordered_images(self):
queryset = self.model.objects.order_by('id').all()
return queryset
def search(self, query_dict):
if isinstance(query_dict, list):
queryset = ImageTag.objects.filter(id__in=query_dict)
if queryset is not None:
return queryset
else:
return False
# Initially getting all objects
queryset_initial = ImageTag.objects.all()
# copying queryset_initial to filter
queryset = queryset_initial
queryset = queryset.filter(company__iexact=query_dict['company']) if query_dict.get('company') not in (
None, '') else queryset
queryset = queryset.filter(accoff__iexact=query_dict['accoff']) if query_dict.get('accoff') not in (
None, '') else queryset
queryset = queryset.filter(section__iexact=query_dict['section']) if query_dict.get('section') not in (
None, '') else queryset
queryset = queryset.filter(docref__iexact=query_dict['docref']) if query_dict.get('docref') not in (
None, '') else queryset
start_date = query_dict.get('start_date')
end_date = query_dict.get('end_date')
if start_date not in (None, '') and end_date not in (None, '') and start_date < end_date:
queryset = queryset.filter(start_date__range=(start_date, end_date))
elif start_date not in (None, ''):
queryset = queryset.filter(start_date__exact=start_date) if query_dict.get('docref') not in (
None, '') else queryset
queryset = queryset.filter(pagenum__iexact=query_dict['pagenum']) if query_dict.get('pagenum') not in (
None, '') else queryset
queryset = queryset.filter(refnum__iexact=query_dict['refnum']) if query_dict.get('refnum') not in (
None, '') else queryset
queryset = queryset.filter(pernum__iexact=query_dict['pernum']) if query_dict.get('pernum') not in (
None, '') else queryset
queryset = queryset.filter(attr1__iexact=query_dict['attr1']) if query_dict.get('attr1') not in (
None, '') else queryset
queryset = queryset.filter(attr2__iexact=query_dict['attr2']) if query_dict.get('attr2') not in (
None, '') else queryset
queryset = queryset.filter(attr3__iexact=query_dict['attr3']) if query_dict.get('attr3') not in (
None, '') else queryset
queryset = queryset.filter(attr4__iexact=query_dict['attr4']) if query_dict.get('attr4') not in (
None, '') else queryset
queryset = queryset.filter(attr5__iexact=query_dict['attr5']) if query_dict.get('attr5') not in (
None, '') else queryset
if len(query_dict.get('tags')) > 0:
tags = query_dict['tags']
queryset = queryset.filter(tags__name__in=[tags])
if queryset != queryset_initial:
return queryset
else:
return []
error
Internal Server Error: /search/
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\scandoc\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\User\PycharmProjects\scandoc\venv\lib\site-packages\django\utils\deprecation.py", line 93, in __call__
response = self.process_response(request, response)
File "C:\Users\User\PycharmProjects\scandoc\venv\lib\site-packages\django\contrib\sessions\middleware.py", line 58, in process_response
request.session.save()
File "C:\Users\User\PycharmProjects\scandoc\venv\lib\site-packages\django\contrib\sessions\backends\db.py", line 83, in save
obj = self.create_model_instance(data)
File "C:\Users\User\PycharmProjects\scandoc\venv\lib\site-packages\django\contrib\sessions\backends\db.py", line 70, in create_model_instance
session_data=self.encode(data),
File "C:\Users\User\PycharmProjects\scandoc\venv\lib\site-packages\django\contrib\sessions\backends\base.py", line 96, in encode
serialized = self.serializer().dumps(session_dict)
File "C:\Users\User\PycharmProjects\scandoc\venv\lib\site-packages\django\core\signing.py", line 87, in dumps
return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type date is not JSON serializable
[06/Feb/2019 16:25:20] "GET /search/?company=&accoff=§ion=&docref=&start_date_month=3&start_date_day=6&start_date_year=1983&end_date_month=10&end_date_day=6&end_date_year=2007&pagenum=&refnum=&pernum=&attr1=&attr2=&attr3=&attr4=&attr5=&tags= HTTP/1.1" 500 109487
Also, this is how I am filtering the records, if someone has a better way of doing it, please share.