简体   繁体   English

在Django中创建用户时如何将用户添加到ManyToManyField?

[英]How to add a user to a ManyToManyField when creating User in Django?

Models.py: Models.py:

class RegularUser(MyUser):
    MyUser.is_staff = False
    MyUser.is_superuser = False

class Meta:
    verbose_name = 'Usuario Regular'
    verbose_name_plural = 'Usuarios Regulares'



class AdminUser(MyUser):
    usuarios = models.ManyToManyField(RegularUser, help_text="Selecciona los usuarios que administra", blank=True)
    MyUser.is_staff = True

class Meta:
    verbose_name = 'Administrador'
    verbose_name_plural = 'Adminsitradores'

I want the next: I log in the admin site as AdminUser, which have staff permission. 我想要下一个:我以具有用户权限的AdminUser登录管理站点。 Then I can create RegularUsers. 然后,我可以创建RegularUsers。 When I create a new RegularUser I want link this Regular User to the AdminUser through the ManyToManyField so this RegularUser owns to the AdminUser. 当我创建一个新的RegularUser时,我想通过ManyToManyField将此常规用户链接到AdminUser,以便此RegularUser拥有AdminUser。 And the AdminUser could manage this RegularUser in the adminSite. AdminUser可以在adminSite中管理此RegularUser。

I want some like this: 我想要这样的东西:

class RegularUserCreationForm(forms.ModelForm): ... ... class RegularUserCreationForm(forms.ModelForm):... ...

@receiver(post_save, sender=RegularUser)
def link_user_admin(sender, instance, created, **kwargs):
    if created:
        instance.adminuser = request.user

But adminuser isn't a field of RegularUser. 但是adminuser不是RegularUser的字段。 And using request in signals is forbidden. 并且禁止在信号中使用请求。 Can someone help me please? 有人能帮助我吗?

Well. 好。 I have found the solution. 我找到了解决方案。 I need to use the request in the post_save method. 我需要在post_save方法中使用该请求。 However, you can't use it in Django signals, so it's possible to use the Django save_model method which you can get access to the request. 但是,您不能在Django信号中使用它,因此可以使用Django save_model方法来访问请求。

    def save_model(self, request, obj, form, change):
        if obj:
            # Save the object
            super().save_model(request, obj, form, change)
            # Add the user instance to M2M field of the request.user (The admin User) who create the RegularUser 
            request.user.adminuser.usuarios.add(obj)

暂无
暂无

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

相关问题 如何为 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? 如何使用 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 中获取带有用户 ID 的 ManyToManyField 对象? - How to get a ManyToManyField object with user IDs in Django? '用户' object is not iterable error django 使用多多多字段时 - 'User' object is not iterable error django when using manytomanyfield Django:如何检查用户是否已经对ManyToManyField进行了投票? - Django: How to check if user has already voted on ManyToManyField? Django,使用添加用户权限创建用户 - Django, creating user with add user permission 使用Django中的ManyToManyField更新用户技能 - Updating a User's skills using ManyToManyField in django 过滤器 model 分配给用户,ManyToManyField - Django - Filter model assigned to user , ManyToManyField - Django 使用带 Angular 和 Django 的 Stripe 创建新用户时,如何添加其他公司字段? - How can I add additional company fields when creating a new user using Stripe with Angular and Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM