[Django]-How to associate invited users with the inviter's Company/group?

5👍

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

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()

Leave a comment