brianjbuck / drf_orjson_renderer Goto Github PK
View Code? Open in Web Editor NEWDjango RestFramework JSON Renderer Backed by orjson
License: MIT License
Django RestFramework JSON Renderer Backed by orjson
License: MIT License
Currently RestFramework does not support Django 4.0 deprecation of pytz
:
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/rest_framework/fields.py:32: in <module>
from pytz.exceptions import InvalidTimeError
E ModuleNotFoundError: No module named 'pytz'
Update to Django 4.0 when Restframework is released.
Today I updated to django 5.0 and got the following warning:
/usr/local/lib/python3.12/site-packages/drf_orjson_renderer/renderers.py:8: RemovedInDjango60Warning: ChoicesMeta is deprecated in favor of ChoicesType.
from django.db.models.enums import ChoicesMeta
Django 6.0 isn't coming until around 2026 so I guess there is no hurry
Could add Travis CI or GitHub actions to automatically run the test suite on all code for each new commit or PR that is submitted.
The drf_ujson2
repository has a good setup that could be used as an example.
My application breaks after upgrading to drf_orjson_render 1.4.0
ERROR django.request:log.py:224 Internal Server Error: /path-replaced/97336477996045/55391693115234
Traceback (most recent call last):
File "/home/falsetru/.virtualenvs/project-name-replaced/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/falsetru/.virtualenvs/project-name-replaced/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/falsetru/work/project-name-replaced-project/targetyo/util/db.py", line 53, in inner
return f(*args, **kwargs)
File "/home/falsetru/work/project-name-replaced-project/targetyo/api/decorators.py", line 36, in wrapper
return func(request, *args, **kwargs)
File "/home/falsetru/.virtualenvs/project-name-replaced/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/falsetru/.virtualenvs/project-name-replaced/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/falsetru/.virtualenvs/project-name-replaced/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/falsetru/.virtualenvs/project-name-replaced/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/falsetru/.virtualenvs/project-name-replaced/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/falsetru/.virtualenvs/project-name-replaced/lib/python3.8/site-packages/rest_framework/views.py", line 497, in dispatch
self.initial(request, *args, **kwargs)
File "/home/falsetru/.virtualenvs/project-name-replaced/lib/python3.8/site-packages/rest_framework/views.py", line 406, in initial
neg = self.perform_content_negotiation(request)
File "/home/falsetru/.virtualenvs/project-name-replaced/lib/python3.8/site-packages/rest_framework/views.py", line 310, in perform_content_negotiation
return conneg.select_renderer(request, renderers, self.format_kwarg)
File "/home/falsetru/.virtualenvs/project-name-replaced/lib/python3.8/site-packages/rest_framework/negotiation.py", line 65, in select_renderer
full_media_type = ';'.join(
TypeError: sequence item 0: expected str instance, NoneType found
I think this is caused by ORJSONRenderer.media_type
change in 9dddb21#diff-af3668e3a8a4fd6a0dcb9a8d9a2550ac2f7fb3e562e2f5f94be73b28b0770684L21
In 1.3:
>>> from drf_orjson_renderer.renderers import ORJSONRenderer
>>> ORJSONRenderer.media_type
'application/json'
In 1.4:
>>> from drf_orjson_renderer.renderers import ORJSONRenderer
>>> ORJSONRenderer.media_type # None
>>>
Packages used:
REST_FRAMEWORK:
REST_FRAMEWORK = {
"DEFAULT_RENDERER_CLASSES": [
"drf_orjson_renderer.renderers.ORJSONRenderer",
"rest_framework.renderers.BrowsableAPIRenderer",
],
"TIME_FORMAT": "%H:%M:%S",
}
Lazy project objects such as lazy translations are not rendered properly. They are not actual strings and have iter method, so rederer renders them as list of chars
Probably I’ve not seen any commits, but what the benefit the python 3.6 support drop provide gave?
Dependency resolution fails when installing drf-orjson-renderer with the latest Django version (4.0.6). The pinned version is too specific.
I would suggest changing it from "django>=3.2,<=4.0"
to "django>=3.2,<4.1"
and re-releasing to PyPi.
how to reproduce?
Just add this reply to any view:
return Response(status=status.HTTP_204_NO_CONTENT)
rest_framework/response.py
ret = renderer.render(self.data, accepted_media_type, context)
will return 'null', and it will end in a malformed http response.
It is not happening on rest_framework.renderers.JSONRenderer
as it has:
def render(self, data, accepted_media_type=None, renderer_context=None):
"""
Render `data` into JSON, returning a bytestring.
"""
if data is None:
return b''
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.