使用表单一次创建多个用户

时间:2017-09-19 07:55:06

标签: django django-models django-forms

我正在努力让我的用户能够创建一个团队并一次创建3个用户来填充该团队。

请你帮我弄清楚它为什么不起作用?

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(已编辑)

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})

我得到一个save()得到了一个意外的关键字参数' commit',我想这是因为提交只适用于ModelsForm我如何保存而不提交才能继续编辑属性?

2 个答案:

答案 0 :(得分:1)

只需创建模型类的实例而不保存并更改其属性。

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})

答案 1 :(得分:0)

通过此行创建并保存MyUser

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

现在user_create它是模型实例,它的save方法没有kwargs commit所以你下一行是额外的只是删除它:

user = user_create.save(commit=False)