[BUG] got an unexpected keyword argument 'instance'
Macilias opened this issue · comments
Describe the bug
The lib fails to instantiate and transmit the object
It fails with the error:
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.name, kwarg))
TypeError: Economics() got an unexpected keyword argument 'instance'
To Reproduce
Steps to reproduce the behavior:
Adopt first example like consumer.py:
class LiveConsumer(ListModelMixin, GenericAsyncAPIConsumer):
queryset = models.Economics.objects.all()
serializer_class = serializers.Economics
permission_classes = (permissions.IsAuthenticated,)
index.hml script:
const request_id = "{{ request.sessions.session_key }}";
const liveSocket = new WebSocket(
'ws://'
+ window.location.host
+ '/ws/project/'
);
liveSocket.onopen = function() {
console.log('WebSockets connection onopen');
liveSocket.send(JSON.stringify({
action: "list",
request_id: request_id,
}));
};
model.py:
class Economics(models.Model):
time_stamp = models.DateTimeField()
cost_capex = models.DecimalField(
max_digits=20, decimal_places=4, default=None, null=True
)
... WITHOUT INIT
serializer:
class EconomicsSerializer(serializers.ModelSerializer):
class Meta:
model = Economics
exclude = []
depth = 1
and routing.py:
websocket_urlpatterns = [
re_path(r"ws/project/$", consumers.LiveConsumer.as_asgi()),
]
Expected behavior
the result of the table is passed as list over the websocet to index.html
LOG
30/03/2022 14:29:26 [ERROR] daphne.server: Exception inside application: Economics() got an unexpected keyword argument 'instance'
Traceback (most recent call last):
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/staticfiles.py", line 44, in __call__
return await self.application(scope, receive, send)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/routing.py", line 71, in __call__
return await application(scope, receive, send)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/sessions.py", line 47, in __call__
return await self.inner(dict(scope, cookies=cookies), receive, send)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/sessions.py", line 263, in __call__
return await self.inner(wrapper.scope, receive, wrapper.send)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/auth.py", line 185, in __call__
return await super().__call__(scope, receive, send)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/middleware.py", line 26, in __call__
return await self.inner(scope, receive, send)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/routing.py", line 150, in __call__
return await application(
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/consumer.py", line 94, in app
return await consumer(scope, receive, send)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/consumer.py", line 58, in __call__
await await_many_dispatch(
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/utils.py", line 51, in await_many_dispatch
await dispatch(result)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/consumer.py", line 73, in dispatch
await handler(message)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/generic/websocket.py", line 194, in websocket_receive
await self.receive(text_data=message["text"])
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/generic/websocket.py", line 257, in receive
await self.receive_json(await self.decode_json(text_data), **kwargs)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/djangochannelsrestframework/consumers.py", line 180, in receive_json
await self.handle_action(action, request_id=request_id, **content)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/djangochannelsrestframework/consumers.py", line 175, in handle_action
await self.handle_exception(exc, action=action, request_id=request_id)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/djangochannelsrestframework/consumers.py", line 135, in handle_exception
raise exc
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/djangochannelsrestframework/consumers.py", line 168, in handle_action
response = await method(request_id=request_id, action=action, **kwargs)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/djangochannelsrestframework/decorators.py", line 85, in async_f
result, status = await database_sync_to_async(func)(self, *args, **_kwargs)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/asgiref/sync.py", line 414, in __call__
ret = await asyncio.wait_for(future, timeout=None)
File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/tasks.py", line 442, in wait_for
return await fut
File "/opt/homebrew/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/channels/db.py", line 13, in thread_handler
return super().thread_handler(loop, *args, **kwargs)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/asgiref/sync.py", line 455, in thread_handler
return func(*args, **kwargs)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/djangochannelsrestframework/mixins.py", line 139, in list
serializer = self.get_serializer(
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/djangochannelsrestframework/generics.py", line 128, in get_serializer
return serializer_class(*args, **kwargs)
File "/Users/maciek/programming/suena/Python/suena/lib/python3.9/site-packages/django/db/models/base.py", line 507, in __init__
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))
TypeError: Economics() got an unexpected keyword argument 'instance'
30/03/2022 14:29:26 [DEBUG] daphne.ws_protocol: WebSocket closed for ['127.0.0.1', 53218]
30/03/2022 14:29:26 [INFO] django.channels.server: WebSocket DISCONNECT /ws/project/ [127.0.0.1:53218]
- OS: [e.g. Ubuntu]
-macOS - Version [e.g. 18.04]
Monterey
Additional context
By the way, the error is independent from the model (which is implied by the error massage) - It happens no matter which Model I try to use:
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))
TypeError: Preset() got an unexpected keyword argument 'instance'
@Macilias sorry for the delay to get back to you on this.
looking over your code it looks like your referencing the the model Economics
as the serializer_class
rather than EconomicsSerializer
your consumer should be:
class LiveConsumer(ListModelMixin, GenericAsyncAPIConsumer):
queryset = models.Economics.objects.all()
serializer_class = serializers.EconomicsSerializer
permission_classes = (permissions.IsAuthenticated,)