我如何访问django allauth自定义文件

时间:2016-09-15 02:04:38

标签: django django-allauth

我想保存并访问allauth的自定义字段。保存表单后尝试访问数据,但它是空的。     {{user.school}} 我只添加forms.py。我是否还需要添加models.py?

- >我改变了模型,但仍然无法访问学校提交的ㅜㅜ)

forms.py     来自django.contrib.auth import get_user_model     来自django进口表格

SCHOOL = (
    ('', 'Select your school...'),
    ('ucla.edu', 'UCLA'),
    ('berkeley.edu', 'Berkeley'),
)

class SignupForm(forms.Form):
    school = forms.ChoiceField(choices=SCHOOL, required=True)

    def signup(self, request, user):
        user.school = self.cleaned_data['school']
        user.save()

models.py
    class UserProfile(models.Model):
        user = models.OneToOneField(User)
        school = models.CharField(max_length=128)

        def __unicode__(self):
            return self.user.username

提前致谢!

1 个答案:

答案 0 :(得分:0)

是肯定的。

我会扩展django用户模型和

profile = user.profile
profile.school = self.cleaned_data['school']
profile.save(update_fields=['school'])