Ben2pop
Ben2pop

Reputation: 756

Creating multiple users at once using Form

I am trying to give the ability to my user to create a team and create 3 users in one time that will populate that team.

Could you please help me to figure out why it is not working please?

forms.py :

class InviteForm(forms.Form):
    Email1 = forms.EmailField()
    Email2 = forms.EmailField()
    Email3 = forms.EmailField()

Views.py

def TeamRegisterTest(request):
    if request.method == "POST":
        form = InviteForm(request.POST)
        if form.is_valid():
            new_user=[]
            for i in form:
                mail = i.value()
                user = MyUser(email = mail)
                user = user_create.save(commit=False)
                password = MyUser.objects.make_random_password()
                user.set_password(password)
                user.is_active = False
                user.is_employee = True
                user.save()
                u1 = user.id
                a1 = MyUser.objects.get(email = request.user.email)
                a2 = Project.objects.filter(project_hr_admin = a1)
                a3 = a2.latest('id')
                a4 = a3.team_id
                a4.members.add(u1)
                current_site = get_current_site(request)
                message = render_to_string('acc_active_email.html', {
                    'user':user,
                    'domain':current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                    'token': account_activation_token.make_token(user),
                })
                mail_subject = 'You have been invited to SoftScores.com please sign in to get access to the app'
                to_email = form.cleaned_data.get('email')
                email = EmailMessage(mail_subject, message, to=[to_email])
                email.send()
                return HttpResponse('An email have been sent to your user asking him to sign in')
        else:
            print("The entered form is not valid")

    else:
        form = InviteForm()
    return render(request,'team_register.html', {'form':form})

views.py (edited)

def TeamRegisterTest(request):
    if request.method == "POST":
        form = InviteForm(request.POST)
        if form.is_valid():
            for i in form:
                mail = i.value()
                user = MyUser(email = mail)
                password = MyUser.objects.make_random_password()
                user.set_password(password)
                user.is_active = False
                user.is_employee = True
                user.save()
                u1 = user.id
                a1 = MyUser.objects.get(email = request.user.email)
                a2 = Project.objects.filter(project_hr_admin = a1)
                a3 = a2.latest('id')
                a4 = a3.team_id
                a4.members.add(u1)
                current_site = get_current_site(request)
                message = render_to_string('acc_active_email.html', {
                    'user':user,
                    'domain':current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                    'token': account_activation_token.make_token(user),
                })
                mail_subject = 'You have been invited to SoftScores.com please sign in to get access to the app'
                to_email = form.cleaned_data.get('email')
                email = EmailMessage(mail_subject, message, to=[to_email])
                email.send()
            return HttpResponse('An email have been sent to each Team member asking them to join in')
        else:
            print("The entered form is not valid")

    else:
        form = InviteForm()
    return render(request,'team_register.html', {'form':form})

I get a save() got an unexpected keyword argument 'commit', I guess that it is because commit is only for ModelsForm how can I save without commiting in order to continue to edit attribute?

Upvotes: 0

Views: 172

Answers (2)

Dima  Kudosh
Dima Kudosh

Reputation: 7376

Just create instance of your model class without saving and change its attributes.

def TeamRegisterTest(request):
    if request.method == "POST":
        form = InviteForm(request.POST)
        if form.is_valid():
            for i in form:
                mail = i.value()
                user = MyUser(email = mail)
                password = MyUser.objects.make_random_password()
                user.set_password(password)
                user.is_active = False
                user.is_employee = True
                user.save()
                # etc.
         else:
            print("The entered form is not valid")

    else:
        form = InviteForm()
    return render(request,'team_register.html', {'form':form})

Upvotes: 0

user8060120
user8060120

Reputation:

by this row you create and save MyUser

user_create = MyUser.objects.create(email = mail)

and now user_create it is model instance and it save method does not have kwargs commit so you next line is extra just remove it:

user = user_create.save(commit=False)

Upvotes: 0

Related Questions