I am trying to allow LDAP users to login to my Django application. Each user needs some additional attributes which I want to store in a User Profile model.
I have implemented the 'post_save' signal to create the userprofile on initial login, however, I found that with LDAP users the created flag is always False even if they have never logged in before.
The only time created = True
is when I create a new superuser using manage.py
My post_save looks like this:
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
print(sender)
print("USERPROFILE1: {0} with id {1}".format(instance, instance.pk))
print("CREATION: {0}".format(created))
if created:
print("USERPROFILE3")
UserProfile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
print("USERPROFILE2")
instance.userprofile.save()
Upon first login, the profile is never created as 'created' is always 'False'
<class 'django.contrib.auth.models.User'>
USERPROFILE1: lbird with id 1
CREATION: False
User has no userprofile. while authenticating
[...]
django.contrib.auth.models.User.userprofile.RelatedObjectDoesNotExist: User has no userprofile.