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

时间:2019-03-04 18:32:24

标签: python django

我正在django中建立聊天,而从django中的Chat模型获取对象时遇到了问题。 对于这些对象,当我尝试访问它时,会得到以下消息: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

以下是相关代码:

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

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

赞赏如何解决它的任何指示! 谢谢

2 个答案:

答案 0 :(得分:3)

拥有Chat实例后,就可以检查请求的用户是否是参与者:

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

答案 1 :(得分:1)

就像错误说的那样,您无法从private IEnumerable<PropertyInfo> GetPropertiesWithoutAttribute<TAttribute>(Type type) where TAttribute : Attribute { return type.GetProperties().Where(p => !p.GetCustomAttributes<TAttribute>().Any()); } 实例访问objects(管理器),而是需要使用实际模型来访问Chat,因此请更改此设置线 objectshttps://github.com/martinlundin/django-multichat-api/blob/master/chat/models.py#L24或删除该行,因为我在代码库中看不到该行的任何使用。

相关问题