具有多个轮廓模型的Django-allauth [英] Django-allauth with multiple profile models

查看:116
本文介绍了具有多个轮廓模型的Django-allauth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django项目,其中有多个配置文件模型,每个都有一个用户模型的外键。它使用 django-allauth 进行注册。



目前,使用社交帐户注册时,用户注册,创建用户和社交帐户,然后将用户重定向到表单以进行填写,具体取决于他/他之前选择的个人资料类型,填写后,该表单是创建的正确的个人资料类型。



如果用户,社交帐户和个人资料类型实例在同一步骤中创建,那么在用户填写个人资料特定表单之后,我想要这样做。有没有我可以做到这一点,而不改变 allauth的代码?通过修改allauth不会太难,但是如果可以帮助,我宁愿不保留第三方应用的自定义副本。



使用自定义适配器,因为它无法访问该请求。

解决方案

看看 allauth.account.signals.user_signed_up 信号,这是在用户(社会或正常)注册后触发的。你可以在那里用你的帐户做你的东西:

  from django.dispatch import receiver 
from allauth.account.signals import user_signed_up

@receiver(user_signed_up)
def do_stuff_after_sign_up(sender,** kwargs):
request = kwargs ['request']
user = kwargs ['用户']
#用你的用户
user.save()

django-allauth repo

 #通常后跟`user_logged_in'(除非电子邮件验证开始)
user_signed_up = Signal(provide_args = [request user])

要了解如何在django中使用信号,请阅读关于它的官方文档



我希望它可以帮助你!



编辑



我很高兴你你的方式通过你的问题,但由于中间件总是加载,我建议使用我提出的方法。来自 django-allauth socialaccount 模块还提供信号的。其中,您可以找到 allauth.socialaccount.signals.social_account_added

 #用户将社交帐户连接到他的本地帐户后发送。 
social_account_added = Signal(provide_args = [request,sociallogin])

With一个类似于之前编写的处理程序,您可以检查用户模型的必需字段,然后调用重定向快捷方式,或返回 HttpResponseRedirect 对象重定向到显示/处理您的表单的视图。


I have a django project in which there are multiple profile models, each having a foreign key to the User model. It uses django-allauth for registration.

Currently, when registering using a social account, the user registers, a User and a socialaccount is created, then the user is redirected to a form to fill out, depending on what profile type s/he chose earlier, and after filling out that form is the correct profile type created.

I'd like it if the user, socialaccount and profile type instances are created in the same step, that is after the user fills out the profile specific form. Is there anyway I can do this without changing allauth's code? It wouldn't be too hard to do this by modifying allauth, but I'd rather not maintain a custom copy of a 3rd party app if it can be helped.

Using a custom adapter is out, because it does not have access to the request.

解决方案

Take a look at the allauth.account.signals.user_signed_up signal, which is triggered just after the user (social or normal) signs up. You can do your stuff with your account there:

from django.dispatch import receiver
from allauth.account.signals import user_signed_up

@receiver(user_signed_up)
def do_stuff_after_sign_up(sender, **kwargs):
    request = kwargs['request']
    user = kwargs['user']
    # Do your stuff with the user
    user.save()

From django-allauth repo:

# Typically followed by `user_logged_in` (unless, e-mail verification kicks in)
user_signed_up = Signal(providing_args=["request", "user"])

To learn how to use signals in django, read the official documentation about it.

I hope it helps you!

EDIT

I'm glad you found your way through your problem, but since middlewares are loaded always, I would suggest using the approach I proposed. The socialaccount module from django-allauth also provide signals. Among them, you can find the allauth.socialaccount.signals.social_account_added:

# Sent after a user connects a social account to a his local account.
social_account_added = Signal(providing_args=["request", "sociallogin"])

With a handler similar to the previously written, you can check the user model for the required fields, and then call the redirect shortcut, or return an HttpResponseRedirect object that redirects to the view that shows/handle your form.

这篇关于具有多个轮廓模型的Django-allauth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆