使用django-allauth保存自定义用户模型

时间:2013-07-13 21:53:47

标签: python django django-models django-allauth

我的django custom user model MyUser还有一个字段:

# models.py
from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
    age = models.PositiveIntegerField(_("age"))

# settings.py
AUTH_USER_MODEL = "web.MyUser"

我还根据to these instructions自定义all-auth Signup form class

# forms.py
class SignupForm(forms.Form):
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)
    age = forms.IntegerField(max_value=100)

    class Meta:
        model = MyUser

    def save(self, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.age = self.cleaned_data['age']
        user.save()

# settings.py
ACCOUNT_SIGNUP_FORM_CLASS = 'web.forms.SignupForm'

提交SignupForm后(属性MyUser.age的字段呈现为核心),我收到此错误:

  

/ accounts / signup / 中的IntegrityError   (1048,“栏'年龄'不能为空”)

存储自定义用户模型的正确方法是什么?

django-allauth:0.12.0; django:1.5.1; Python 2.7.2

3 个答案:

答案 0 :(得分:7)

虽然有点晚了但是如果有人帮助的话。

您需要通过继承DefaultAccountAdapter并设置

来创建自己的自定义AccountAdapter
class UserAccountAdapter(DefaultAccountAdapter):

    def save_user(self, request, user, form, commit=True):
        """
        This is called when saving user via allauth registration.
        We override this to set additional data on user object.
        """
        # Do not persist the user yet so we pass commit=False
        # (last argument)
        user = super(UserAccountAdapter, self).save_user(request, user, form, commit=False)
        user.age = form.cleaned_data.get('age')
        user.save()

您还需要在设置中定义以下内容:

ACCOUNT_ADAPTER = 'api.adapter.UserAccountAdapter'

如果您有一个自定义SignupForm在用户注册期间创建其他模型,并且您需要创建一个原子事务来阻止任何数据保存到数据库,除非所有数据都成功,这也很有用。

django-allauth的DefaultAdapter会保存用户,因此如果您在自定义SignupForm的save方法中出现错误,用户仍会保留在数据库中。

因此,对于遇到此问题的任何人,您的CustomAdpater将会是这样的

类UserAccountAdapter(DefaultAccountAdapter):

    def save_user(self, request, user, form, commit=False):
        """
        This is called when saving user via allauth registration.
        We override this to set additional data on user object.
        """
        # Do not persist the user yet so we pass commit=False
        # (last argument)
        user = super(UserAccountAdapter, self).save_user(request, user, form, commit=commit)
        user.age = form.cleaned_data.get('age')
        # user.save() This would be called later in your custom SignupForm

然后,您可以使用@transaction.atomic

修饰自定义SignupForm
@transaction.atomic
def save(self, request, user):
    user.save() #save the user object first so you can use it for relationships
    ...

答案 1 :(得分:1)

旁注

使用Django 1.5自定义用户模型,最佳做法是使用get_user_model函数:

from django.contrib.auth import get_user_model

# forms.py
class SignupForm(forms.Form):
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)
    age = forms.IntegerField(max_value=100)

    class Meta:
        model = get_user_model() # use this function for swapping user model

    def save(self, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.age = self.cleaned_data['age']
        user.save()

# settings.py
ACCOUNT_SIGNUP_FORM_CLASS = 'web.forms.SignupForm'

也许它没有关系,但我认为值得注意。

答案 2 :(得分:0)

我认为您应该在SignupForm中的Meta类中定义fields属性并设置包含age的字段列表,如下所示:

WinWaitActive("","Authentication Required","120")
 If WinExists("","Authentication Required") Then
 Send("username{TAB}")
 Send("password{Enter}")
 EndIf

如果它不起作用,请看看 this