Json is not serializable in django - django

def ajax_leave_type_data(request):
pk = request.GET.get('id', None)
is_taken= Leave_Types.objects.filter(user_id=pk)
return HttpResponse(json.dumps(is_taken), content_type='application/json')
Thats the error i am getting everytime please give some solution if anyone face this problem before i tried many ways but unable to solve this Thanks in advance
Internal Server Error: /ajax/leave_ty
Traceback (most recent call last):
File "/mnt/e/Leave_Management/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/mnt/e/Leave_Management/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/mnt/e/Leave_Management/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/mnt/e/L_Management/Leave_Management_App/views.py", line 209, in ajax_leave_type_data
return HttpResponse(json.dumps(is_taken), content_type='application/json')
File "/usr/lib/python3.5/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.5/json/encoder.py", line 179, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <QuerySet [<Leave_Types: Casual leaves>, <Leave_Types: Unpaid leaves>, <Leave_Types: Privilege leaves>, <Leave_Types: Sick leaves>]> is not JSON serializable
[13/Oct/2018 17:07:20] "GET /ajax/leave_ty?id=52&csrfmiddlewaretoken=3Fxb3n2PBMRSYWOgPLgRSWZzvQANFiFLTDihrZJ0HklK02YPU39C7eSGY5su7QKX HTTP/1.1" 500 15445

I would return it using Django's JsonResponse
from django.http import JsonResponse
def ajax_leave_type_data(request):
pk = request.GET.get('id', None)
is_taken= Leave_Types.objects.filter(user_id=pk)
return JsonReponse({'is_taken': is_taken})

Related

requests.post(url, data)error `Response`, `HttpResponse` or `HttpStreamingResponse` but received a `<class 'tuple'>`

I am doing my first Django MongoDB project and trying to save the data set in the database
here is the create method that I'm using
def create(self, request, *args, **kwargs):
try:
client = MongoClient('srv13.absolute.ag:27017')
collection = client.farmos.device
header = request.headers['Authorization']
req = json.loads(request.body)
resp = requests.post(url=f"{settings.MAINFLUX_URL}/things", data=req, params=args, headers=header)
device_id = resp.headers['Location'][8:]
req['device_token'] = device_id
collection.insert_one(req)
return {'data': 'Success'}, 200
except Exception as e:
return {'data': str(e)}, 400
But it showing an error
and the error is
Internal Server Error: /api/v1/device/
Traceback (most recent call last):
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/adarsh/farmos/farmos/persistance_layer/mongodb/device_operations.py", line 53, in create
response = requests.post(url=f"{settings.MAINFLUX_URL}/things", data=json.dumps(req), headers=header)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/requests/api.py", line 117, in post
return request('post', url, data=data, json=json, **kwargs)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/requests/sessions.py", line 515, in request
prep = self.prepare_request(req)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/requests/sessions.py", line 453, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/requests/models.py", line 319, in prepare
self.prepare_headers(headers)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/requests/models.py", line 451, in prepare_headers
for header in headers.items():
AttributeError: 'str' object has no attribute 'items'
I think there is a problem in this line
python
resp = requests.post(url=f"{settings.MAINFLUX_URL}/things", data=req, params=args, headers=header)
I have tried different thing changing the type of req and header to solve it but couldn't do it

Test django view which has post request

I am trying to test my django views which contain post request.
def save_movie(request):
'''
Fetch data and save to Movie model
data obtained from post request is a string so need to clean it and convert to json
'''
if request.method == 'POST':
movie_data = str(request.POST['movie_data'])
movie_data = movie_data.replace("\'","\"")
b=""
for i in range(len(movie_data)):
if movie_data[i] is "\"" and movie_data[i-1].isalpha() and movie_data[i+1].isalpha():
b+="\'"
else:
b+=movie_data[i]
json_data = json.loads(b)
new_movie, created = Movie.objects.get_or_create(title=json_data['title'], episode_id=int(json_data['episode_id']), opening_crawl=json_data['opening_crawl'], director=json_data['director'], producer=json_data['producer'], release_date=json_data['release_date'], characters=str(json_data['characters']), planets=str(json_data['planets']), starships=str(json_data['starships']), vehicles=str(json_data['vehicles']), species=str(json_data['species']), created=parse_datetime(json_data['created']), edited=parse_datetime(json_data['edited']), url=json_data['url'])
if created:
new_movie.save()
return HttpResponse('<h1>Movie Saved</h1>')
else:
return HttpResponse('<h1>Movie already Saved</h1>')
and test.py is
def test_save_movie(self):
response = self.client.post(self.save_movie_url, {'title':'test', 'episode_id':1, 'opening_crawl':'test for movie', 'director':'director', 'producer':'producer', 'release_date':'test_date', 'characters':'test_chars', 'planets':'test_planets', 'starships':'test_starships', 'vehicles':'str_vehicles', 'species':'str_species', 'created':'2014-12-09T13:50:49.641000Z', 'edited':'2015-12-09T13:50:49.641000Z', 'url':'https://test.com'})
print(response.status_code)
when i run this testcase i get MultiValueDictError
File "/home/abhishek/Desktop/SpotDraft/StarWars/favourites/tests/test_views.py", line 58, in test_save_movie
response = self.client.post(self.save_movie_url, {'title':'test', 'episode_id':1, 'opening_crawl':'test for movie', 'director':'director', 'producer':'producer', 'release_date':'test_date', 'characters':'test_chars', 'planets':'test_planets', 'starships':'test_starships', 'vehicles':'str_vehicles', 'species':'str_species', 'created':'2014-12-09T13:50:49.641000Z', 'edited':'2015-12-09T13:50:49.641000Z', 'url':'https://test.com'})
File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/test/client.py", line 526, in post
response = super().post(path, data=data, content_type=content_type, secure=secure, **extra)
File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/test/client.py", line 356, in post
secure=secure, **extra)
File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/test/client.py", line 421, in generic
return self.request(**r)
File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/test/client.py", line 496, in request
raise exc_value
File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/abhishek/Desktop/SpotDraft/StarWars/favourites/views.py", line 109, in save_movie
movie_data = str(request.POST['movie_data'])
File "/home/abhishek/Desktop/SpotDraft/env/lib/python3.7/site-packages/django/utils/datastructures.py", line 78, in __getitem__
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'movie_data'
It seems it cant find movie_data which should be present in my post request as the view extracts it from there
How to solve this?

Unicode Exception thrown when other Exception occurs

I have this strange behavior in my django project (1.11) running (example from the dev server): first I get a logging error, which might have to do with the setup
However, the real strange thing is: whenever an exception is thrown, this causes a Unicode error. I am assuming there might be a problem with the language setup or python version interference?
This is the view - I added the zero division to cause the error. When I remove it, everything works fine:
class TestView(TemplateView):
template_name = "table.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
dummy=1/0
return context
Here the error log:
--- Logging error ---
Traceback (most recent call last):
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 155, in get
context = self.get_context_data(**kwargs)
Traceback (most recent call last):
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 155, in get
context = self.get_context_data(**kwargs)
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 49868)
Traceback (most recent call last):
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 155, in get
context = self.get_context_data(**kwargs)
Unhandled exception in thread started by <bound method Thread._bootstrap of <Thread(Thread-1, started daemon -173327552)>>
Traceback (most recent call last):
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/site-packages/django/views/generic/base.py", line 155, in get
context = self.get_context_data(**kwargs)
File "/home/franzritt/sky/monitoring/views.py", line 19, in get_context_data
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/logging/__init__.py", line 978, in emit
msg = self.format(record)
File "/usr/lib/python3.4/logging/__init__.py", line 828, in format
return fmt.format(record)
File "/usr/lib/python3.4/logging/__init__.py", line 573, in format
record.exc_text = self.formatException(record.exc_info)
File "/usr/lib/python3.4/logging/__init__.py", line 523, in formatException
traceback.print_exception(ei[0], ei[1], tb, None, sio)
File "/usr/lib/python3.4/traceback.py", line 169, in print_exception
for line in _format_exception_iter(etype, value, tb, limit, chain):
File "/usr/lib/python3.4/traceback.py", line 153, in _format_exception_iter
yield from _format_list_iter(_extract_tb_iter(tb, limit=limit))
File "/usr/lib/python3.4/traceback.py", line 18, in _format_list_iter
for filename, lineno, name, line in extracted_list:
File "/usr/lib/python3.4/traceback.py", line 65, in _extract_tb_or_stack_iter
line = linecache.getline(filename, lineno, f.f_globals)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/linecache.py", line 15, in getline
lines = getlines(filename, module_globals)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/linecache.py", line 41, in getlines
return updatecache(filename, module_globals)
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/linecache.py", line 127, in updatecache
lines = fp.readlines()
File "/home/franzritt/.virtualenvs/sky/lib/python3.4/codecs.py", line 313, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 957: invalid continuation byte
I have shown here the - as I think - important parts of the error log. The logging error and the unicode error show up many more times. So my basic problem is, whenever I get an exepction I get 20 Unicode expections and 10 logging errors, which makes it difficult to find the real exception.
Your model str is not handling Unicode correctly. I recommend you put the UTF-8 encoding. To do this, put this at the beginning of you .py module/file:
# -*- coding: utf-8 -*-
If that does not work, try with:
def __str__(self):
return self.name.encode('utf8')
Or with:
class TestView(TemplateView):
template_name = "table.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
dummy=1/0
return context.encode('utf-8')

build_filters() got an unexpected keyword argument 'ignore_bad_filters'

I just updated my code from django 1.8 to 1.11 and so i also had to update tasty pie. Before update everything worked fine but now I am getting this error on one of my tests.
Here is the test case
def test_does_not_return_restricted_fields_when_insufficient_permissions(self):
self.user.user_permissions.all().delete()
response = self.api_client.get(url,
format='json',
authentication=self.create_apikey(username=self.username, api_key=self.api_key),
data=data)
Here is the full Trace,
Traceback (most recent call last):
File "/spare/local/projects/towerportal/apps/exchangedb/tests/test_api.py", line 68, in test_does_not_return_restricted_fields_when_insufficient_permissions
response = self.get_response(self.detail_url)
File "/spare/local/projects/towerportal/apps/exchangedb/tests/test_api.py", line 26, in get_response
data=data)
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/test.py", line 70, in get
return self.client.get(uri, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/django/test/client.py", line 536, in get
**extra)
File "/spare/local/venv/lib/python2.7/site-packages/django/test/client.py", line 340, in get
return self.generic('GET', path, secure=secure, **r)
File "/spare/local/venv/lib/python2.7/site-packages/django/test/client.py", line 416, in generic
return self.request(**r)
File "/spare/local/venv/lib/python2.7/site-packages/django/test/client.py", line 501, in request
six.reraise(*exc_info)
File "/spare/local/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/spare/local/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/spare/local/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/spare/local/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 185, in inner
return func(*args, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/resources.py", line 221, in wrapper
response = callback(request, *args, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/resources.py", line 470, in dispatch_detail
return self.dispatch('detail', request, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/resources.py", line 493, in dispatch
response = method(request, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/resources.py", line 1376, in get_detail
obj = self.cached_obj_get(bundle=basic_bundle, **self.remove_api_resource_names(kwargs))
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/resources.py", line 1195, in cached_obj_get
cached_bundle = self.obj_get(bundle=bundle, **kwargs)
File "/spare/local/venv/lib/python2.7/site-packages/tastypie/resources.py", line 2176, in obj_get
applicable_filters = self.build_filters(filters=kwargs, ignore_bad_filters=True)
TypeError: build_filters() got an unexpected keyword argument 'ignore_bad_filters'
Could your please paste your tastypie resource code?
Without related code, I can only guess that it is because you override
def build_filters(): in your modelresource class, but the newer version of tastypie added a ignore_bad_filter parameter in the function definition which you do not have in your modelresource code.
The build_filters function declaration in the latest tastypie:
def build_filters(self, filters=None, ignore_bad_filters=False):
The old version probably does not have the ignore_bad_filter parameter. To fix this, just add ignore_bad_filters=Falsekey word parameter to the build_filters function in your modelresource class.

Error must be convertible to a buffer, not InMemoryUploadedFile

I am trying to save a blob image (jpeg) and getting the following error:
Traceback (most recent call last):
File "/Users/Shah/Envs/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/Shah/Envs/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Shah/Envs/env/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/Shah/Desktop/project/project/views.py", line 604, in thumbnail_photo
image_result.write(blob)
TypeError: must be convertible to a buffer, not InMemoryUploadedFile
[01/Aug/2016 12:20:55] "POST /thumbnail_photo HTTP/1.1" 500 15608
After doing some googling, I couldn't find a solution. I am not sure how to convert the InMemoryUploadedFile to a buffer. The relevant lines of code from the method are below.
if request.FILES:
print('files')
blob = request.FILES['blob']
image_result = open('what_is_happening.jpeg', 'wb')
image_result.write(blob)
return HttpResponse("success")
Thank you!
Change
image_result.write(blob)
to
image_result.write(blob.read())