Reputation: 53
I am building a chat in django and I have a problem getting objects from the Chat model in django.
For the objects I get a traceback with the message: Manager isn't accessible via Chat instances
when I try to access it.
Traceback:
Exception inside application: Manager isn't accessible via Chat instances
File "/Users/fokusiv/Projects/django-multichat-api/venv/lib/python3.6/site-packages/channels/sessions.py", line 179, in __call__
return await self.inner(receive, self.send)
File "/Users/fokusiv/Projects/django-multichat-api/venv/lib/python3.6/site-packages/channels/middleware.py", line 41, in coroutine_call
await inner_instance(receive, send)
File "/Users/fokusiv/Projects/django-multichat-api/venv/lib/python3.6/site-packages/channels/consumer.py", line 59, in __call__
[receive, self.channel_receive], self.dispatch
File "/Users/fokusiv/Projects/django-multichat-api/venv/lib/python3.6/site-packages/channels/utils.py", line 52, in await_many_dispatch
await dispatch(result)
File "/Users/fokusiv/Projects/django-multichat-api/venv/lib/python3.6/site-packages/asgiref/sync.py", line 108, in __call__
return await asyncio.wait_for(future, timeout=None)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py", line 339, in wait_for
return (yield from fut)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/Users/fokusiv/Projects/django-multichat-api/venv/lib/python3.6/site-packages/channels/db.py", line 13, in thread_handler
return super().thread_handler(loop, *args, **kwargs)
File "/Users/fokusiv/Projects/django-multichat-api/venv/lib/python3.6/site-packages/asgiref/sync.py", line 123, in thread_handler
return self.func(*args, **kwargs)
File "/Users/fokusiv/Projects/django-multichat-api/venv/lib/python3.6/site-packages/channels/consumer.py", line 105, in dispatch
handler(message)
File "/Users/fokusiv/Projects/django-multichat-api/venv/lib/python3.6/site-packages/channels/generic/websocket.py", line 39, in websocket_connect
self.connect()
File "/Users/fokusiv/Projects/django-multichat-api/chat/consumers.py", line 60, in connect
is_participant_in_chat(self.room_name, self.scope['user'])
File "/Users/fokusiv/Projects/django-multichat-api/chat/models.py", line 24, in is_participant_in_chat
test = chat.objects
File "/Users/fokusiv/Projects/django-multichat-api/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 176, in __get__
raise AttributeError("Manager isn't accessible via %s instances" % cls.__name__)
Manager isn't accessible via Chat instances
Here is relevant code:
chat/model.py
from django.shortcuts import get_object_or_404
from django.db import models
import uuid as makeuuid
from users.models import Usern
def is_participant_in_chat(chatid, userid):
chat = get_object_or_404(Chat, uuid=chatid)
test = chat.objects
#Check if userid is in participants
return False
class Chat(models.Model):
uuid = models.UUIDField(primary_key=True, default=makeuuid.uuid4, editable=False)
name = models.CharField(max_length=50, blank=True)
participants = models.ManyToManyField(Usern, related_name='chats')
def __str__(self):
return str(self.uuid)
users/model.py
from django.db import models
import uuid as makeuuid
import os
from django.contrib.auth.models import AbstractUser
class Usern(AbstractUser):
uuid = models.UUIDField(primary_key=True, default=makeuuid.uuid4, editable=False)
name = models.CharField(max_length=50)
def __str__(self):
return str(self.uuid)
Complete project can be found here: https://github.com/martinlundin/django-multichat-api
Appreciate any pointers of how to solve it! Thanks
Upvotes: 1
Views: 618
Reputation: 2249
Just like the error says, you can't access the objects
(Manager) from a Chat
instance instead you need to use the actual model to access the objects
therefore change this line
https://github.com/martinlundin/django-multichat-api/blob/master/chat/models.py#L24
to test = Chat.objects.all()
or remove that line since I don't see any use of it currently in the codebase.
Upvotes: 1
Reputation: 1323
Once you have the Chat
instance, you can check if the requested user is a participant:
def is_participant_in_chat(chatid, userid):
chat = get_object_or_404(Chat, uuid=chatid)
return chat.participants.filter(uuid=userid).exists()
Upvotes: 3