django-allauth: how to properly use email_confirmed signal to set user to active

前端 未结 2 941
予麋鹿
予麋鹿 2020-12-14 11:46

In another post, I mentioned that I was trying to use allauth\'s email_confirmed signal to change the is_active field on the confirmed user to true. However, the following c

相关标签:
2条回答
  • 2020-12-14 12:04

    Turns out the email_address returned by the email_confirmed signal isn't actually a string containing the address, but an object -- allauth.account.models.EmailAddress. This wasn't very clear at all from the documentation, but glad it's resolved now. The code that ended up working was:

    @receiver(email_confirmed)
    def email_confirmed_(request, email_address, **kwargs):
    
        user = User.objects.get(email=email_address.email)
        user.is_active = True
    
        user.save()
    
    0 讨论(0)
  • 2020-12-14 12:12

    I found this page while trying to set up a signal from AllAuth to my CustomUser Model.

    After some debugging and continued searching, I found an old GitHub thread on the subject: https://github.com/pennersr/django-allauth/issues/347. His solution worked for me.

    I changed the location of my receiver function from app/signals.py to app/models.py

    Apparently, by default the ready() function of the AppConfig Class imports the models.py file, but a signals.py file needs to be manually added to your apps.py file:

    from django.apps import AppConfig
    
    
    class NameOfAppConfig(AppConfig):
        name = 'NameOfApp'
    
        def ready(self):
            import NameOfApp.signals
    

    Anyway, it's easy to miss this in the documentation. I'd guess in future Django releases they will include a signals.py file as a default file and incorporate it automatically.

    0 讨论(0)
提交回复
热议问题