0

I'd like to save and access custom field of allauth. After I save the form and try to access the data but it's empty. {{ user.school }} I only add forms.py. Do I need to add on models.py too?

-> I change the models but still I can't access the school filed ㅜㅜ)

forms.py from django.contrib.auth import get_user_model from django import forms

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

Thanks in advance!

MrKarl
  • 19
  • 1
  • 3
  • You need to extend the user model. https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#specifying-a-custom-user-model – cdvv7788 Sep 15 '16 at 03:12

1 Answers1

0

yes.

I would extend the django user model and

profile = user.profile
profile.school = self.cleaned_data['school']
profile.save(update_fields=['school'])
doniyor
  • 36,596
  • 57
  • 175
  • 260
  • Hi @doniyor, when I change the forms.py is shown error 'User' object has no attribute 'profile'. Any help what i missed? – MrKarl Sep 15 '16 at 04:18
  • @MrKarl you need to extend user model by creating new userprofile model with onetoone relation. more: https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#extending-the-existing-user-model – doniyor Sep 15 '16 at 08:03