How to save an abstractuser using multiple forms

76 views Asked by At

I have 2 forms: 1 usercreation form and one that contains the verified file. I want to add 2 fields(verified,is_doctor) with the first form and save it to create a abstractuser. I have tried the following but it ends up blank in my db.

{'verified': <UploadedFile: app.jpg (image/jpeg)>}

userCreate.verified=form_list[1].cleaned_data.get('verified')
userCreate.is_doctor=True

views.py

from django.core.files.storage import FileSystemStorage
import os
from django.conf import settings
class DoctorWizard(SessionWizardView):
    file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, '/doctor/'))
    template_name = "registration/signup.html"
    form_list = [SignUpForm,verify]
    
    def done(self, form_list, **kwargs):
        process_data(form_list)
        userCreate = form_list[0]

        userCreate.verified=form_list[1].cleaned_data.get('verified')
        userCreate.is_doctor=True
        
        userCreate.save()
        username = userCreate.cleaned_data.get('username')
        raw_password = userCreate.cleaned_data.get('password1')
        user = authenticate(username=username, password=raw_password)
        if user:
            auth_login(self.request, user)
        return redirect('home')

forms.py

class SignUpForm(UserCreationForm):
    first_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    last_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')

    class Meta:
        model = Profile
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )

class verify(forms.Form):
    verified = forms.ImageField(required=True)
    class Meta:
        model = Profile
        fields = ('verified',)
    

models.py

class Profile(AbstractUser):
    bio = models.TextField(max_length=100, blank=True)
    phone_number = PhoneNumberField(max_length=25, region="US")
    birth_date = models.DateField(blank = True, null = True) 
    is_doctor = models.BooleanField(default=False)
    verified = models.ImageField(upload_to='media/doctor')
    date_created = models.DateTimeField(auto_now_add=True)
    avatar = models.ImageField(default='default.png', upload_to='')
    #status = models.BooleanField(default=False)
1

There are 1 answers

0
Arundeep Chohan On
def done(self, form_list, **kwargs):
        process_data(form_list)
        userCreate = form_list[0]
        userCreate.save()
        username = userCreate.cleaned_data.get('username')
        raw_password = userCreate.cleaned_data.get('password1')
        user = authenticate(username=username, password=raw_password)
        if user:
            user.verified=form_list[1].cleaned_data.get('verified')
            user.is_doctor=True
            user.save()
            auth_login(self.request, user)
        return redirect('home')

All I did was set the fields after I created the user.