question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError: 'NoneType' object has no attribute 'get'

See original GitHub issue

Hi, 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:closed
  • Created 7 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
abdulhaq-ecommented, Sep 10, 2016

400: Received document does not contain primary data

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.

There was no need to json.dumps(data) thing, why we need it here ?

The renderer does that for you when passing format rather than content_type. To confirm this, go back to DRF serializers and views and try post or patch without json.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.

1reaction
abdulhaq-ecommented, Sep 10, 2016

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, only data is passed to this method. There is no renderer_context which then raises an error when using drf-json-api renderer. I gave the JSONRenderer in DRF a quick look and it seems that it works fine if its render method isn’t given a renderer_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 to post leads to a different path where renderer.render is not called, see here

The 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found