vintasoftware / drf-rw-serializers Goto Github PK
View Code? Open in Web Editor NEWGeneric views, viewsets and mixins that extend the Django REST Framework ones adding separated serializers for read and write operations
License: MIT License
Generic views, viewsets and mixins that extend the Django REST Framework ones adding separated serializers for read and write operations
License: MIT License
Describe what you were trying to get done.
Tell us what happened, what went wrong, and what you expected to happen.
Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
Describe what you were trying to get done.
Tell us what happened, what went wrong, and what you expected to happen.
As document said: the view class is base on generics.ListCreateAPIView. and from drf_rw_serializers import generics
But I am using viewsets.ModelViewSet for view class base, how could I enable drf_rw_serializers to my view class?
Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
If yes, any possibility to add support of type hints
? as DRF supports type hints but with the usage of this library, all the type checks vanished
When I add swagger/openapi view support (with help of drf-yasg for example) I get undesirable/incorrect specification for writable methods (POST, PUT, PATCH), they correspond to read_serializer_class
instead of to write_serializer_class
.
from drf_rw_serializers import viewsets
class MyViewSet(viewsets.ModelViewSet):
read_serializer_class = serializers.ReadSerializer
write_serializer_class = serializers.WriteSerializer
To fix that problem I suggest to add something along the following lines:
def get_serializer_class(self):
if self.request.method in ['GET']:
return self.get_read_serializer_class()
return self.get_write_serializer_class()
Hopefully I provided sufficient amount of information.
Thank you for your efforts.
We are currently in the middle of migration to RW serializers, meaning some viewsets has get_read/write_serializer_class
implemented and some using old approach (1 serializer) which end-up being served from get_serializer_class
. With the latest changes if the class implements get_serializer_class
and does not implement serializer_class
attribute, then assertion error raised.
Below I will try to post the simple code snippet
class MyApiViewSet(drf_rw_serializers_viewsets.GenericViewSet[MyModel]):
queryset = MyModel.objects.all()
@typing_extensions.override
def get_serializer_class(self) -> type[drf_serializers.BaseSerializer[MyModel]:
serializer_class:type[drf_serializers.BaseSerializer[MyModel] | None = None
if self.action == 'action_one':
serializer_class = SomeSerializer
if serializer_class is None:
msg = f'Unknown action: {self.action}'
raise ValueError(msg)
return serializer_class
This snippet is very good to find out if you are missing any serializer for any action (especially it is done during schema generation).
This code was not producing any errors in 1.1.x
version but produces this in 1.2.x+
:
AssertionError: 'MyApiViewSet' should either include one of `serializer_class` and
`read_serializer_class` attribute, or override one of the `get_serializer_class()`,
`get_read_serializer_class()` method.
We do override get_serializer_class
but apparently in the RW serializer sources it is not checked.
UPDATE:
Okay, the issue is a little bit different.
In our codebase we are calling self.get_wirte_serializer
(note that we do not have write_serializer_class
attribute, we do not have get_write_serializer_class
method, we rely only on get_serializer_class
in this case.
Stack trace is the following:
File "/home/user/app/src/application/view.py", line 123, in some_method
serializer = self.get_write_serializer(data=data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/venv/lib/python3.11/site-packages/drf_rw_serializers/generics.py", line 96, in get_write_serializer
serializer_class = self.get_write_serializer_class()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/venv/lib/python3.11/site-packages/drf_rw_serializers/generics.py", line 109, in get_write_serializer_class
return self._get_serializer_class()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/venv/lib/python3.11/site-packages/drf_rw_serializers/generics.py", line 23, in _get_serializer_class
self.serializer_class is not None
Please note that the error is about read
somehow:
'MyApiViewSet' should either include one of `serializer_class` and `read_serializer_class` attribute, or override one of the `get_serializer_class()`, `get_read_serializer_class()` method.
Read/write serializers not being called. I've traced it into generics.py, but don't see it going through the mixins ever...
I implemented drf-rw-serializers, put them in installed apps, imported them and went through your usage documentation.
Quit the server with CONTROL-C.
Internal Server Error: /
Traceback (most recent call last):
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/rest_framework/views.py", line 494, in dispatch
response = self.handle_exception(exc)
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/rest_framework/views.py", line 454, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/rest_framework/views.py", line 491, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/rest_framework/schemas/views.py", line 31, in get
schema = self.schema_generator.get_schema(request, self.public)
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/rest_framework/schemas/generators.py", line 286, in get_schema
links = self.get_links(None if public else request)
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/rest_framework/schemas/generators.py", line 324, in get_links
link = view.schema.get_link(path, method, base_url=self.url)
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/rest_framework/schemas/inspectors.py", line 182, in get_link
fields += self.get_serializer_fields(path, method)
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/rest_framework/schemas/inspectors.py", line 302, in get_serializer_fields
serializer = view.get_serializer()
File "/Users/user/workspace/django-dapi/lib/python3.6/site-packages/rest_framework/generics.py", line 112, in get_serializer
return serializer_class(*args, **kwargs)
TypeError: 'NoneType' object is not callable
example_app
an error appears such as 500 internal server error
example_app
but I am still instructed to migrate againAny suggest or idea to help with this? Thanks before
Since Python 3.7 is the latest Python release, it would be great if the projects tox
and Travis configs were updated to test on it.
The contents of LICENSE.txt
and setup.py
are not in agreement about which license this project is released under.
LICENSE.txt
contains the MIT license text, while the Trove classifiers in setup.py
(which are used to tag/classify the package on PyPI) claims the license is AGPL 3.0. These licenses are not compatible - it would be prudent to resolve this, as many projects would avoid dependencies that are licensed with AGPL.
When support for Django 2.2 released?
Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
Describe what you were trying to get done.
Tell us what happened, what went wrong, and what you expected to happen.
Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
When we are trying to update the dependency (dependabot), it is failing because it wants to update djangorestframework==3.15.1
as well. We are not upgrading as we have some breaking changes there.
There is no indication that exactly this version is required and before there were no issues. I guess the setup with poetry brings something...
As drf-rw-serializers
does not depend on specific version of DRF, installation of new version of the library should not try to update DRF version.
NOTE: we are using ruff's
UV, but I guess it should not be the reason...
(venv) server@ad9c683d6e80:~/app$ uv pip freeze | grep -E "(drf-rw-serializers=|djangorestframework=)"
djangorestframework==3.14.0
drf-rw-serializers==1.1.1
(venv) server@ad9c683d6e80:~/app$ uv pip install drf_rw_serializers==1.2.0
Resolved 5 packages in 322ms
Installed 2 packages in 22ms
- djangorestframework==3.14.0
+ djangorestframework==3.15.1
- drf-rw-serializers==1.1.1
+ drf-rw-serializers==1.2.0
(venv) server@ad9c683d6e80:~/app$ uv pip freeze | grep -E "(drf-rw-serializers=|djangorestframework=)"
djangorestframework==3.15.1
drf-rw-serializers==1.2.0
I think there's not much gain in pinning an upper version requirement for Django specially since it's a minor version. I think we should either remove the upper limit or make it <4.0
Hi there! So the problem is mentioned in the tittle of the issue.
Requests from terminal works fine, like http GET http://127.0.0.1:8000/api/v1/users/
and POST aswell,
but in browser instead of standard DRF page i get TypeError.
Environment:
Request Method: GET
Request URL: http://localhost:8000/api/v1/users/
Django Version: 3.2.7
Python Version: 3.8.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'users_app.apps.UsersAppConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/home/andy/programming/drf/users/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/andy/programming/drf/users/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 204, in _get_response
response = response.render()
File "/home/andy/programming/drf/users/env/lib/python3.8/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/home/andy/programming/drf/users/env/lib/python3.8/site-packages/rest_framework/response.py", line 70, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
File "/home/andy/programming/drf/users/env/lib/python3.8/site-packages/rest_framework/renderers.py", line 724, in render
context = self.get_context(data, accepted_media_type, renderer_context)
File "/home/andy/programming/drf/users/env/lib/python3.8/site-packages/rest_framework/renderers.py", line 655, in get_context
raw_data_post_form = self.get_raw_data_form(data, view, 'POST', request)
File "/home/andy/programming/drf/users/env/lib/python3.8/site-packages/rest_framework/renderers.py", line 554, in get_raw_data_form
serializer = view.get_serializer()
File "/home/andy/programming/drf/users/env/lib/python3.8/site-packages/rest_framework/generics.py", line 110, in get_serializer
return serializer_class(*args, **kwargs)
Exception Type: TypeError at /api/v1/users/
Exception Value: 'NoneType' object is not callable
from django.contrib.auth.models import User
from users_app.serializers import ReadOnlyUserSerializer, WriteOnlyUserSerializer
from drf_rw_serializers import generics
class UsersList(generics.ListCreateAPIView):
queryset = User.objects.all()
read_serializer_class = ReadOnlyUserSerializer
write_serializer_class = WriteOnlyUserSerializer
Serializers and other app code must be fine, since it works correctly with usual generic classes.
Adding serializer_class = ReadOnlyUserSerializer
line to my views solves the problem, but this solution seems weird to me.
I've read docs about installation and usage at https://drf-rw-serializers.readthedocs.io/en/latest/,
still can't figure out what am i missing.
Any help appreciated.
drf-rw-serializers version:
drf-rw-serializers==1.0.1
Django version:
Django==1.11.13
django-filter==1.1.0
Python version:
2.7.10
Operating System:
Mac OS 10.14.2
When I run runserver to start my app, it reports:
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/init.py", line 37, in import_module
import(name)
ImportError: No module named drf-rw-serializers
run : pip install drf-rw-serializers
Add 'drf-rw-serializers', to INSTALLED_APPS of setting file.
Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
Hello in the documentation site it states that there is a version 1.0.5 that supports Django 4 but it's not available on pypi.org. Can you please release it?
Thank you
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.