python 3.6 JsonResponse issue - python

So i recently migrated to Python 3.6 and Django 1.11 and my JsonResponse code looked like this:
return JsonResponse({'status': '1'})
it worked fine but after the migration i started getting this error:
TypeError: Object of type 'bytes' is not JSON serializable
After printing the type of the data passed to JsonResponse i realized python 3.6 changed this from dict to byte.
So i changed the code to make sure i was passing a dict.
I still get the same error after trying all of this:
data = dict([('status', 0)])
print(data)
print(type(data))
# print(type(json.dumps(data)))
# data = {"status": '0'}
# data = json.dumps(data)
# json.dumps(data.decode("utf-8"))
#response = json.JSONEncoder().encode({"status": 0})
#JsonResponse(data, safe=False)
# response = json.dumps(data)
print(JsonResponse(data, safe=False))
return JsonResponse(data, safe=False)
Prints:
{'status': 0}
<class 'dict'>
<JsonResponse status_code=200, "application/json">
with the json.dumps options y get this error instead
AttributeError: 'str' object has no attribute 'get'
Any help would be much appreciated
Traceback
Traceback (most recent call last):
File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/core/handlers/base.py", line 131, in get_response
response = middleware_method(request, response)
File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/middleware.py", line 58, in process_response
request.session.save()
File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 81, in save
return self.create()
File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 54, in create
self.save(must_create=True)
File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 83, in save
obj = self.create_model_instance(data)
File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 69, in create_model_instance
session_data=self.encode(data),
File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py", line 98, in encode
serialized = self.serializer().dumps(session_dict)
File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/core/signing.py", line 93, in dumps
return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable

The problem isn't return JsonResponse({'status': '1'}).
The traceback is showing you that the error occurs when Django tries to save the Django session.
You must be doing something like this in the view:
request.session['my_key'] = b'bytes'
For that example, you would have to decode the bytes object (or use a string instead):
request.session['my_key'] = b'bytes'.decode('utf-8')

Related

Add json response to a list

I am new in learning python requests and I am using GET METHOD and I am trying to insert returned json content in list, But it is showing
TypeError: Object of type bytes is not JSON serializable
I have tried many times and I also tried by adding into dictionary.
views.py
def request_get(request):
url = "http://127.0.0.1:8000/api/items/"
response = requests.get(url)
results = []
results.append(response.content)
return redirect('home')
But it is showing TypeError: Object of type bytes is not JSON serializable
I have also tried by using :-
results = []
results.append({
'response' : response.content
})
Full Traceback
Traceback (most recent call last):
File "D:\apps - app\app\env\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "D:\apps - app\app\env\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\app - app - app\env\lib\site-packages\django\http\response.py", line 653, in __init__
data = json.dumps(data, cls=encoder, **json_dumps_params)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "D:\app - app - app\env\lib\site-packages\django\core\serializers\json.py", line 106, in default
return super().default(o)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
But it also didn't worked for me.
Any help would be much Appreciated. Thank You in Advance
You need to use results.append(response.json()) to convert to JSON
Or try:
import json
results.append(json.loads(response.content.decode("utf-8")))

using request api into a json file that i can iterate through

ok so im using python 3
i was able to get the data of the api using print(endpoint.json())
but i want to make it readable with pandas, so i can iterate through it easier.
this is the code (keep in mind i discarded my own api key and im using rapid api as a resource (specificly the movie database)
import requests
import json
import pandas
url = "https://movies-tvshows-data-imdb.p.rapidapi.com/"
querystring = {"type":"get-popular-movies","page":"1","year":"2020"}
headers = {
'x-rapidapi-host': "movies-tvshows-data-imdb.p.rapidapi.com",
'x-rapidapi-key': my key
}
response = requests.request("GET", url, headers=headers, params=querystring)
data=response.json()
df=pandas.read_json(data)
print(df)
i get this error
Traceback (most recent call last):
File "c:\Users\Home\Documents\studying\newproject\newproject.py", line 15, in <module>
df=pandas.read_json(data)
File "C:\Users\Home\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\util\_decorators.py", line 199, in wrapper
return func(*args, **kwargs)
File "C:\Users\Home\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\util\_decorators.py", line 296, in wrapper
return func(*args, **kwargs)
File "C:\Users\Home\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\io\json\_json.py", line 593, in read_json
filepath_or_buffer, _, compression, should_close = get_filepath_or_buffer(
File "C:\Users\Home\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\io\common.py", line 243, in get_filepath_or_buffer
raise ValueError(msg)
ValueError: Invalid file path or buffer object type: <class 'dict'>
In your case data is a dict.
So, try with:
pandas.DataFrame.from_dict(data)

Python Json Error: ValueError: No JSON object could be decoded

I'm getting an error when I execute this script and can't figure it out.
The Error:
Traceback (most recent call last):
File "./upload.py", line 227, in <module>
postImage()
File "./upload.py", line 152, in postImage
reddit = RedditConnection(redditUsername, redditPassword)
File "./upload.py", line 68, in __init__
self.modhash = r.json()['json']['data']['modhash']
File "/usr/lib/python2.6/site-packages/requests/models.py", line 799, in json
return json.loads(self.text, **kwargs)
File "/usr/lib/python2.6/site-packages/simplejson/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.6/site-packages/simplejson/decoder.py", line 335, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.6/site-packages/simplejson/decoder.py", line 353, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
You are getting this exception because you are using the wrong json function here:
def getNumberOfFailures(path):
try:
with open(path + '.failurecount') as f:
return json.loads(f.read())
except:
return 0
You need to do this instead:
def getNumberOfFailures(path):
try:
with open(path + '.failurecount') as f:
return json.load(f)
except:
return 0
json.loads() is used on json strings. json.load() is used on json file objects.
As some people have mentioned, you need to reissue yourself a new API key and delete the one you posted here in your code. Other people can and will abuse those secret keys to spam Reddit under your name.

EncodeError: 'QuerySet' object has no attribute '_prefetch_related_lookups'

I recently updated my version of django from 1.2.5 to 1.7. Once done, all new transactions on my app were working as expected. However whenever I try to access a pickled object, I get the error
EncodeError: 'QuerySet' object has no attribute '_prefetch_related_lookups'
Here is the error thrown
'QuerySet' object has no attribute '_prefetch_related_lookups'
Traceback (most recent call last):
File "/foo/bar/gateway/baseGateway.py", line 108, in queueMessage
eng.processMessage(msgRow)
File "/foo/bar/engine/processor.py", line 101, in processMessage
tasks.deliverMessage.apply_async(args=[foo, bar], queue='message-deliver')
File "/opt/bitnami/python/lib/python2.7/site-packages/celery/app/task.py", line 555, in apply_async
**dict(self._get_exec_options(), **options)
File "/opt/bitnami/python/lib/python2.7/site-packages/celery/app/base.py", line 353, in send_task
reply_to=reply_to or self.oid, **options
File "/opt/bitnami/python/lib/python2.7/site-packages/celery/app/amqp.py", line 305, in publish_task
**kwargs
File "/opt/bitnami/python/lib/python2.7/site-packages/kombu/messaging.py", line 161, in publish
compression, headers)
File "/opt/bitnami/python/lib/python2.7/site-packages/kombu/messaging.py", line 237, in _prepare
body) = dumps(body, serializer=serializer)
File "/opt/bitnami/python/lib/python2.7/site-packages/kombu/serialization.py", line 164, in dumps
payload = encoder(data)
File "/opt/bitnami/python/lib/python2.7/contextlib.py", line 35, in __exit__
self.gen.throw(type, value, traceback)
File "/opt/bitnami/python/lib/python2.7/site-packages/kombu/serialization.py", line 59, in _reraise_errors
reraise(wrapper, wrapper(exc), sys.exc_info()[2])
File "/opt/bitnami/python/lib/python2.7/site-packages/kombu/serialization.py", line 55, in _reraise_errors
yield
File "/opt/bitnami/python/lib/python2.7/site-packages/kombu/serialization.py", line 164, in dumps
payload = encoder(data)
File "/opt/bitnami/python/lib/python2.7/site-packages/kombu/serialization.py", line 356, in pickle_dumps
return dumper(obj, protocol=pickle_protocol)
File "/opt/bitnami/python/lib/python2.7/site-packages/django/db/models/query.py", line 113, in __reduce__
return super(QuerySet, self).__reduce__()
File "/opt/bitnami/python/lib/python2.7/copy_reg.py", line 84, in _reduce_ex
dict = getstate()
File "/opt/bitnami/python/lib/python2.7/site-packages/django/db/models/query.py", line 91, in __getstate__
self._fetch_all()
File "/opt/bitnami/python/lib/python2.7/site-packages/django/db/models/query.py", line 967, in _fetch_all
if self._prefetch_related_lookups and not self._prefetch_done:
EncodeError: 'QuerySet' object has no attribute '_prefetch_related_lookups'
Looking at some of the solutions offered online and by django here and here, I cleared the sessions table in django to no avail.The error still persists. I use memcache in my application too and i cleared that. I also use celery.
Anyone know how to fix this?
I was seeing a related issue when trying to change a queryset on a model form within a view. The error was:
'NoneType' object has no attribute '_prefetch_related_lookups'
forms.py
class S1Form(forms.ModelForm):
library = forms.ModelChoiceField(
queryset = Library.objects.all(),
to_field_name = 'title',
required = True,
widget = forms.Select(
attrs = {
'class': 'custom-select'}
),
disabled = False,
empty_label = 'Select a library'
)
views.py
class FilteredSpectraSearchListView(SingleTableMixin, FilterView):
...
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['sform'] = S1Form()
context['sform'].fields['library'].queryset = None
if <something>:
context['sform'].fields['library'].queryset = <...>
elif <something-else>:
context['sform'].fields['library'].queryset = <...>
return context
The goal was to have an empty queryset initially which is later changed based on a few conditional statments. The problem was that the conditional "<something>" was not firing and None remained the queryset. The solution was simply to provide an empty queryset rather than None for this case:
...
context['sform'].fields['library'].queryset = Library.objects.none()
if <something>:
context['sform'].fields['library'].queryset = <...>
elif <something-else>:
context['sform'].fields['library'].queryset = <...>
...
Googling giving us a results
resetting sessions app fixed the issue (at least for the moment...)
https://code.djangoproject.com/ticket/18674
The problem is the Sessions, I had to delete them all for it to work. Addtionally I changed the settings SECRET_KEY so all sessions don’t validate.
http://jj.isgeek.net/2013/04/django-queryset-object-has-no-attribute-_prefetch_related_lookups/
You have some serialized data from Django < 1.4, and Kombu tries to deserialize it in your current Django version.
I don't know where Kombu saves its serialized data, but that's where you should look. You should either delete the stale data, or if you need to keep the data, manually change it to match your current Django version.

Python Whoosh AttributeError when updating index

I'm trying to update my whoosh search index when I make changes to my database, but keep getting an error that I haven't been able to figure out.
One of my views:
from search import update_index
#view_config(route_name='update_effect_brief', permission='edit')
def update_effect_brief(request):
name = request.matchdict['pagename']
brief = request.params['effect_brief']
effect = Effect.get_by_name(name) # This is an "effect" object
effect.update_brief(brief)
update_index(effect)
return HTTPFound(location=request.route_url('view_effect', pagename=name))
My search.py file:
from whoosh.index import create_in, open_dir
def update_index(obj):
'''Update single ingredient, product or effect.'''
index = open_dir('searchindex') # searchindex is the name of the directory
with index.searcher as searcher: # crashes on this line!
writer = index.writer()
update_doc(writer, obj)
Traceback:
Traceback (most recent call last):
File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.9-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 152, in toolbar_tween
response = _handler(request)
File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.9-py2.7.egg/pyramid_debugtoolbar/panels/performance.py", line 55, in resource_timer_handler
result = handler(request)
File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid-1.4.5-py2.7.egg/pyramid/tweens.py", line 21, in excview_tween
response = handler(request)
File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid_tm-0.7-py2.7.egg/pyramid_tm/__init__.py", line 82, in tm_tween
reraise(*exc_info)
File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid_tm-0.7-py2.7.egg/pyramid_tm/__init__.py", line 63, in tm_tween
response = handler(request)
File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid-1.4.5-py2.7.egg/pyramid/router.py", line 161, in handle_request
response = view_callable(context, request)
File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid-1.4.5-py2.7.egg/pyramid/config/views.py", line 237, in _secured_view
return view(context, request)
File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid-1.4.5-py2.7.egg/pyramid/config/views.py", line 377, in viewresult_to_response
result = view(context, request)
File "/home/home/SkinResearch/env/local/lib/python2.7/site-packages/pyramid-1.4.5-py2.7.egg/pyramid/config/views.py", line 493, in _requestonly_view
response = view(request)
File "/home/home/SkinResearch/skinresearch/skinproject/views.py", line 544, in update_effect_brief
update_index(effect)
File "/home/home/SkinResearch/skinresearch/skinproject/search.py", line 37, in start_update
update_index(obj)
File "/home/home/SkinResearch/skinresearch/skinproject/search.py", line 92, in update_index
with index.searcher as searcher:
AttributeError: __exit__
What am I doing wrong?
You need to call the index.searcher() method to create the context manager:
with index.searcher() as searcher:
See the Searcher object section in the Whoosh quickstart, and The Searcher object documentation.
It isn't entirely clear to me why you are creating a searcher, but then create a writer in the block and update the index. Perhaps you wanted to use the writer as a context manager instead here:
with index.writer() as writer:
update_doc(writer, obj)
and leave the searcher for searching operations.

Categories

Resources