Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix: TypeError: object of type 'NoneType' has no len()#1121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
ericinfins wants to merge2 commits intodjango-json-api:mainfromericinfins:patch-1

Conversation

ericinfins
Copy link

TypeError: object of type 'NoneType' has no len()

Fixes #

Changed the sequence

Description of the Change

Checklist

  • PR only contains one change (considered splitting up PR)
  • unit-test added
  • documentation updated
  • CHANGELOG.md updated (only for user relevant changes)
  • author name inAUTHORS

Fix the error:  TypeError: object of type 'NoneType' has no len()
@sliverc
Copy link
Member

Thanks for bringing this up. The check is odd, and makes sense to do it the other way around. However, I am not sure whether it is at all possible (or should be possible) that data is None. Could you share how you have encountered this error, data being None?

@ericinfins
Copy link
Author

ericinfins commentedJan 23, 2023
edited
Loading

I got the issue when I create the Response object without any data

In my API, I use the following line to return to the front:

Response(status=201)

In this line when self.data is None

class Response(SimpleTemplateResponse):....@property    def rendered_content(self):          .....          ret = renderer.render(self.data, accepted_media_type, context) # this line the self.data is None

These are the logs

[Tue Jan 17 13:07:11.098182 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976] ERROR 2023-01-17 13:07:11,096 log 22877 140608666109696 Internal Server Error: /django/test/some_id/actions/xxx/[Tue Jan 17 13:07:11.098258 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976] Traceback (most recent call last):[Tue Jan 17 13:07:11.098278 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner[Tue Jan 17 13:07:11.098283 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     response = get_response(request)[Tue Jan 17 13:07:11.098290 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/django/core/handlers/base.py", line 145, in _get_response[Tue Jan 17 13:07:11.098296 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     response = self.process_exception_by_middleware(e, request)[Tue Jan 17 13:07:11.098300 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/django/core/handlers/base.py", line 143, in _get_response[Tue Jan 17 13:07:11.098304 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     response = response.render()[Tue Jan 17 13:07:11.098308 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/django/template/response.py", line 105, in render[Tue Jan 17 13:07:11.098313 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     self.content = self.rendered_content[Tue Jan 17 13:07:11.098317 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/rest_framework/response.py", line 70, in rendered_content[Tue Jan 17 13:07:11.098321 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     ret = renderer.render(self.data, accepted_media_type, context)[Tue Jan 17 13:07:11.098325 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/rest_framework_json_api/renderers.py", line 544, in render[Tue Jan 17 13:07:11.098337 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     return self.render_errors(data, accepted_media_type, renderer_context)[Tue Jan 17 13:07:11.098342 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/rest_framework_json_api/renderers.py", line 529, in render_errors[Tue Jan 17 13:07:11.098373 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     utils.format_errors(data), accepted_media_type, renderer_context[Tue Jan 17 13:07:11.098377 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/rest_framework_json_api/utils.py", line 388, in format_errors[Tue Jan 17 13:07:11.098381 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     if len(data) > 1 and isinstance(data, list):[Tue Jan 17 13:07:11.098388 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976] TypeError: object of type 'NoneType' has no len()

@sliverc
Copy link
Member

Do you think you can write/add a test which reproduces this behavior? The issue is even though your fix looks simple the result is actually an invalid error object according to theJSON:API spec as an error object may not be None. Once there is a reproducing test, it will be easier to figure out what a complete bugfix would be for this issue.

ericinfins reacted with thumbs up emoji

@sliverc
Copy link
Member

This has been around for a while. Closing it for now as not being able to process this PR. In case you get around, adding a test to reproduce your error, simply ping me.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@ericinfins@sliverc

[8]ページ先頭

©2009-2025 Movatter.jp