gpt4 book ai didi

python - Django:无法通过 MyModel 实例访问管理器

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:57 26 4
gpt4 key购买 nike

我在 Django 中构建聊天,但在从 Django 中的聊天模型获取对象时遇到问题。对于这些对象,我得到一个带有消息的回溯:Manager isn't accessible via Chat instances 当我尝试访问它时。

回溯:

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

相关代码如下:

聊天/模型.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)

用户/模型.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)

完整的项目可以在这里找到:https://github.com/martinlundin/django-multichat-api

感谢任何解决方法的指点!谢谢

最佳答案

一旦你有了 Chat 实例,你就可以检查请求的用户是否是参与者:

def is_participant_in_chat(chatid, userid):
chat = get_object_or_404(Chat, uuid=chatid)
return chat.participants.filter(uuid=userid).exists()

关于python - Django:无法通过 MyModel 实例访问管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54989425/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com