简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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