2017-08-30 21 views
2

我正在尋找使用我創建的帳戶模型來處理有關用戶的財務數據。我如何確保django-allauth在註冊用戶時創建此模型,並通過新用戶和帳戶模型的關鍵字鏈接?在創建用戶時使用多個模型

回答

1

您可以實現自定義管道。首先修改SOCIAL_AUTH_PIPELINE在Django settings文件是這樣的:

SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details', 
'social.pipeline.social_auth.social_uid', 
'social.pipeline.social_auth.auth_allowed', 
'social.pipeline.social_auth.social_user', 
'social.pipeline.user.get_username', 
'social.pipeline.user.create_user', 
'social.pipeline.social_auth.associate_user', 
'social.pipeline.social_auth.load_extra_data', 
'social.pipeline.user.user_details', 
'yourapp.pipeline.add_account' 
) 

然後在yourapp創建pipeline模塊,也可以使用已有的模型該模型中創建功能add_account與下面的代碼:

def add_account(backend, details, response, user=None, is_new=False, *args, **kwargs): 
    if is_new: 
     Account.objects.create(user=user) 

你可以查找更多詳情herehere

UPDATE

管道只是功能至極將由Django的allauth期間autentication過程中被調用。所以你可以在這個過程中添加一些自定義函數。

+0

我不熟悉管道是如何工作的,你能提供更多的細節嗎? –

+0

@AlexCreamer檢查我的更新。 – neverwalkaloner

+0

你怎麼把用戶=用戶創建賬戶?我不只需要使用PK? –

相關問題