![](/img/trans.png)
[英]Create User and UserProfile on user signup with django-allauth
[英]django-allauth receiver signal to add group permission to user on signup
我的目標是在用戶注冊時向他們添加一個新組。 我正在使用 django-allauth 包,似乎最好的方法是使用包定義的信號。 不完全確定如何調試它,因為我沒有收到任何錯誤消息。
信號.py
from allauth.account.signals import user_signed_up
from django.dispatch import receiver
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
#Group Added To New Users: "Can Add Pattern, Symbol, Broker"
@receiver(user_signed_up)
def user_signed_up_signal_handler(request, user):
group = Group.objects.get(name='Can Add Pattern, Symbol, Broker')
user.groups.add(group)
user.save()
不是最好的答案,但我能夠通過將完全相同的代碼放在我的models.py 文件而不是signals.py 中來解決這個問題。
我假設您正在做一些事情來加載signals.py 而不僅僅是期望它被自動加載? 如果不是,那么每個https://docs.djangoproject.com/en/3.1/topics/signals/的建議是在應用程序的 ready() 函數中導入信號。
/apps/core/__init__.py
default_app_config = 'apps.core.apps.CoreConfig'
/apps/core/apps.py
from django.apps import AppConfig
class CoreConfig(AppConfig):
name = 'apps.core'
def ready(self):
import apps.core.signals
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.