且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将受邀用户与邀请人的公司/组相关联?

更新时间:2023-12-01 16:44:16

我必须实现一个Signal在Django。它侦听用户注册,然后查看该用户是否在邀请模型中。如果是这样,它将查找邀请者的公司,并将其与注册用户相关联。

I had to implement a Signal in django. It listens for a user signing up, then looks to see if that user is in the Invitation model. If so, it looks up the inviter's company and associates that with the user signing up.

init .py

default_app_config = "users.apps.UsersConfig"

signals.py

signals.py

from allauth.account.signals import user_signed_up
from django.dispatch import receiver

from invitations.utils import get_invitation_model

@receiver(user_signed_up)
def user_signed_up(request, user, **kwargs):
    try:
        Invitation = get_invitation_model()
        invite = Invitation.objects.get(email=user.email)
    except Invitation.DoesNotExist:
        print("this was probably not an invited user.")
    else:
        user.company = invite.inviter.company
        user.save()