AttributeError: 'NoneType' object has no attribute 'get'
See original GitHub issueHi, there
Since yesterday, I’m stuck on this error in my test script:
Creating test database for alias 'default'...
E
======================================================================
ERROR: test_create_account (api.accounts.tests.test_crud.AccountTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/app/api/accounts/tests/test_crud.py", line 22, in test_create_account
response = self.client.post(url, data)
File "/usr/local/lib/python3.5/site-packages/rest_framework/test.py", line 169, in post
path, data=data, format=format, content_type=content_type, **extra)
File "/usr/local/lib/python3.5/site-packages/rest_framework/test.py", line 90, in post
data, content_type = self._encode_data(data, format, content_type)
File "/usr/local/lib/python3.5/site-packages/rest_framework/test.py", line 65, in _encode_data
ret = renderer.render(data)
File "/usr/local/lib/python3.5/site-packages/rest_framework_json_api/renderers.py", line 419, in render
view = renderer_context.get("view", None)
AttributeError: 'NoneType' object has no attribute 'get'
----------------------------------------------------------------------
Ran 1 test in 0.019s
FAILED (errors=1)
Destroying test database for alias 'default'...
Particularly, this is the line of my code causing the error:
response = self.client.post(url, data)
And this is the line of your code that responsible of the error
view = renderer_context.get("view", None)
Simply because renderer_context
is None
All my test was passing before switching to this library (using default DRF renders)
Here is my configurations:
REST_FRAMEWORK = {
'PAGE_SIZE': 5,
'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
'DEFAULT_PAGINATION_CLASS':
'rest_framework_json_api.pagination.PageNumberPagination',
'DEFAULT_PARSER_CLASSES': (
'rest_framework_json_api.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
),
'DEFAULT_RENDERER_CLASSES': (
'rest_framework_json_api.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
),
'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
'TEST_REQUEST_RENDERER_CLASSES':(
'rest_framework_json_api.renderers.JSONRenderer',
),
'TEST_REQUEST_PARSER_CLASSES':(
'rest_framework_json_api.parsers.JSONParser',
),
'TEST_REQUEST_DEFAULT_FORMAT' : 'vnd.api+json',
}
Am I doing something wrong ?
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
Why do I get AttributeError: 'NoneType' object has no attribute ...
NoneType means that instead of an instance of whatever Class or Object you think you're working with, you've actually got None .
Read more >AttributeError: 'NoneType' object has no attribute 'get'
The Python "AttributeError: 'NoneType' object has no attribute 'get'" occurs when we try to call the get() method on a None value, e.g....
Read more >How to fix AttributeError: 'NoneType' object has no attribute 'get'
AttributeError means that there was an Error that had to do with an Attribute request. In general, when you write x.y, y is...
Read more >How do I fix : attributeerror: 'nonetype' object has no attribute ...
When ever you get a problems that involves a message such as " 'nonetype' object has no attribute ..." it means the same...
Read more >AttributeError: 'NoneType' object has no ... - Python Forum
The problem is that i get this error: Quote: AttributeError: 'NoneType' object has no attribute 'get'. What i do wrong?
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For creating/updating resources, you have to send a resource object which has to exist under a top level member named
data
. This is a requirement in JSON API specs.The renderer does that for you when passing
format
rather thancontent_type
. To confirm this, go back to DRF serializers and views and trypost
orpatch
withoutjson.dumps(data)
:response = self.client.post(url, data, content_type='application/vnd.api+json')
An error will be raised.
I believe the renderer in DRF Json API can be enhanced to allow tests such as yours.
I think the solution I gave you in the previous comment will work.
In DRF test module, the
render
method of arenderer
instance is being called here. As you can see, onlydata
is passed to this method. There is norenderer_context
which then raises an error when usingdrf-json-api renderer
. I gave theJSONRenderer
in DRF a quick look and it seems that it works fine if itsrender
method isn’t given arenderer_context
. In fact, I think all renderers in DRF behave the same with regards to this aspect. Perhaps it was designed like this for testing purposes. They all have the following line in common:renderer_context = renderer_context or {}
Anyway, if what I’m saying is correct, passing
content_type
directly topost
leads to a different path whererenderer.render
is not called, see hereThe tests written for this package either used the standard Django Unittest or pytest and some used the testing classes provided by DRF. For the latter,
content_type
was used when sending data.