简体   繁体   English

Django:将用户添加到组中

[英]Django: Adding User in a Group

Here's the model, 这是模型

class Groupchat(models.Model):
    admin = models.ForeignKey(User, related_name="admin")
    members = models.ManyToManyField(User, related_name="members", blank=True)
    name = models.CharField(max_length=50)

I've no idea how can 'admin' add any user to the Group that he/she created. 我不知道如何“管理员”将任何用户添加到他/她创建的组中。

How can I do that or at least what is the right way to do so? 我该怎么做,或者至少是正确的方法呢?

Please helpme! 请帮我!

Well, there is a simple way to do it 好吧,有一种简单的方法

new_user = User.objects.get(pk=1) #get the user
group = Groupchat.objects.get(pk=1) #get the group
group.members.add(new_user)

things can get more sophisticated as the system grows, but starting with a simple solution and evolving whats is necessary is a good idea 随着系统的发展,事情会变得越来越复杂,但是从一个简单的解决方案开始,发展必要的东西是一个好主意。

if only the group admin can add persons, all you have to do is on the request check the admin used_id session to see if the id = group_admin id, if is proceed to the query above 如果只有组管理员可以添加人员,则您需要做的只是在请求上检查admin used_id会话,以查看id = group_admin id,如果继续上述查询

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM