清理后的数据未达到模型 - django rest auth

时间:2016-03-21 00:37:51

标签: python django django-authentication django-rest-auth

我尝试使用django-rest-auth将数据从angularJS应用传递到django。

在我的控制台中,我可以看到数据正确到达(特别是'性别'以及' yob')。

然而,似乎在" cleaning_data"被调用 - 数据不会在我的django用户模型中结束

注意 - 我设置了两个用户模型(一个是股票django用户表,另一个是我在models.py中创建的用户表)

from allauth.account.adapter import DefaultAccountAdapter  # refer to allauth.account.adapter
from stashd.models import User, AccountProfile
import datetime, json


class MyAccountAdapter(DefaultAccountAdapter):

    def save_user(self, request, user, form, commit=True):

        system_account = super(StashdAccountAdapter, self).save_user(request, user, form, commit)
        print form
## Print form shows all the data, but the cleaned_data get's rid of it
        data = form.cleaned_data
        first_name = data.get('first_name')
        username = data.get('username')
        last_name = data.get('last_name')
        email = data.get('email')
        # gender = data.get('gender')
        # yob = data.get('yob') 
        # dob = datetime.date(int(yob), 1, 1)
        info = {}
        # for k, v in [('first', username), ('first', first_name), ('last', last_name), ('email', email)]:
        #     if v:
        #         info[k] = v
        for k, v in [('email', email)]: #,('dob',dob),('gender',gender)
            if v:
                info[k] = v

        user = User(**info)
        user.save()

        AccountProfile(user=user, account=system_account).save()

        return system_account

2 个答案:

答案 0 :(得分:0)

您需要在表单上调用form.is_valid()来填充cleaning_data

答案 1 :(得分:0)

在django文档中声明,

  

使用一组数据创建Form实例并进行验证后   它,您可以通过其cleaning_data属性

访问干净的数据

您可以从here参考文档。