简体   繁体   English

如何在 Django 中获取带有用户 ID 的 ManyToManyField 对象?

[英]How to get a ManyToManyField object with user IDs in Django?

I have a little built-in Chat with Django Channels and I created a Chat model where I can store the messages (it is like a CHAT ROOM with ID) and I want to get the single object by the 2 users ID.我有一些内置的 Django 频道聊天,我创建了一个聊天模型,我可以在其中存储消息(就像一个带 ID 的聊天室),我想通过 2 个用户 ID 获取单个对象。

I tried something like Chat.objects.filter(participiants__in=[ID1,ID2]) but it just returns every single chat they are in.我尝试了 Chat.objects.filter(participiants__in=[ID1,ID2]) 之类的东西,但它只返回他们所在的每个聊天。

def post(self,request):

        sender = UserProfile.objects.get(user_id=request.user.id)

        message = request.data.get('message')
        post_id = request.data.get('post_id')
        recipient_id = request.data.get('recipient_id')

        chat_room = Chat.objects.filter(participiants__in=[sender, recipient_id])

class Chat(models.Model):
    participiants = models.ManyToManyField(UserProfile, related_name="chats")
    messages = models.ManyToManyField(Message, blank=True, related_name="messages")

    def get_room_messages(self):
        messages = self.messages.order_by('-timestamp').all()
        return [[i.text,i.sender_id,i.link,i.post_id] for i in messages]

You should then count the number of participants after filtering, and check if that is equal to 2 , like:然后您应该计算过滤后的participants数量,并检查它是否等于2 ,例如:

from django.db.models import Count

chat_rooms = Chat.objects.filter(
    participiants__in=[sender, recipient_id]
).annotate(
    nparticipants=Count('participiants')
).filter(nparticipants=2)

If there is exactly one such Chat object, you can use .get(..) instead:如果只有一个这样的Chat对象,您可以使用.get(..)代替:

from django.db.models import Count

chat_rooms = Chat.objects.filter(
    participiants__in=[sender, recipient_id]
).annotate(
    nparticipants=Count('participiants')
).get(nparticipants=2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Django 信号从 Django 中的 manytomanyfield 获取用户并使用接收的 manytomany 用户字段创建另一个对象 - How to get the user from a manytomanyfield in Django using Django singals and creating another object with the recived manytomany user field 如何在 Django RestFramework API 中将多字段返回为 object - How to return manytomanyfield as object in Django RestFramework API 如何从 ManyToManyField 获取数据 - Django - How to get data from ManyToManyField - Django Django:如何获取模型字段是否为ManyToManyField - Django: how to get model field is ManyToManyField or not '用户' object is not iterable error django 使用多多多字段时 - 'User' object is not iterable error django when using manytomanyfield 在Django中创建用户时如何将用户添加到ManyToManyField? - How to add a user to a ManyToManyField when creating User in Django? Django:如何检查具有ManyToManyField的对象是否包含其他对象ManyToManyField的全部内容? - Django: How to check if object with ManyToManyField contains the entire contents of other object ManyToManyField? Django:如何检查用户是否已经对ManyToManyField进行了投票? - Django: How to check if user has already voted on ManyToManyField? 如何为 django 中的团队成员将 request.user 添加到 manytomanyfield? - How to add request.user to manytomanyfield for team members in django? 如何添加到默认 Django 用户 model 的 ManyToManyField 扩展? - How can you add to a ManyToManyField extension of the default Django User model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM