djangodjango-modelsdjango-allauthdjango-custom-user

Custom user model object not created after allauth signup


I'm trying to create a custom user object (Author model) right after a new user signs up using allauth's signal, the signup works fine and the User is created but the object (Author) is not being created.

This is my Author (custom user) model:

class Author(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.user.first_name + ' ' + self.user.last_name

This is what I have in my view:

@receiver(user_signed_up)
def after_user_signed_up(request, user):
    author = Author.objects.create(user=user)

Any idea what I might be doing wrong? Should I be setting the custom user differently to make this work? I'll be adding more fields to the Author model later on and I saw that doing a one-to-one relation should be the way to go, but not sure why this isn't working.

Thanks in advance!


Solution

  • I figured out the answer - I needed to, either:

    1. move the function into my model.py module, or
    2. keep function in my view or in a signals.py module, but manually add that module to my apps.py file:
    from django.apps import AppConfig
    
    
    class NameOfAppConfig(AppConfig):
        name = 'NameOfApp'
    
        def ready(self):
            import NameOfApp.signals