简体   繁体   English

Django外键-第二种形式

[英]Foreign Key in Django - second Form

I have a second form that have to be filed after the first. 我有第二份表格,必须在第一份表格之后提交。 However I'm not able to save the relation between both models, unless I give the option for the person that have filing the forms chose the email for the first form.My idea is to give this information directly on the back end, but I cant do that. 但是我无法保存这两个模型之间的关系,除非我为提交表单的人提供选择,选择了第一个表单的电子邮件。我的想法是直接在后端提供此信息,但是我无法做到这一点。

My models are too big to post all here, so if any can help me with a generic idea I will be glad. 我的模型太大了,无法在此处发布所有内容,因此,如果有什么可以帮助我提出一个通用想法的,我将感到高兴。

I'm using in both forms a ModelForm in Django. 我在Django中都使用两种形式的ModelForm。

my Models: User Person Volunteer - Client - Staff 我的模型:用户人员志愿者-客户-员工

Than I have 3 types of volunteers: Admin; 我有3种类型的志愿者:管理员; Program; 程序; Committee; 委员会; After I fill the Volunteer-Person-User model, I need to go to the next form for Volunteer Admin or Volunteer Program or Volunteer Committee; 填写“志愿者-用户-用户”模型后,我需要进入“志愿者管理”或“志愿者计划”或“志愿者委员会”的下一个表格;

because the user uses the email as a primary key also, thats the value when I create the foreign key from my admin program and the modelform give me the choices to the email. 因为用户也将电子邮件用作主键,所以这就是我从管理程序创建外键时的值,而modelform给了我选择电子邮件的权限。 but on the database, only keeps the id for the reference. 但在数据库上,仅保留ID供参考。

my first view that's handling the first form: 我的第一个视图正在处理第一种形式:

def volunteer_form(request):

if request.method == 'POST':
    form = VolunteerForm(request.POST)
    if form.is_valid():
        saving = form.save(commit=False)
        saving.password = make_password(form.cleaned_data['password'])
        saving.save()
        item = saving.id
        if saving.person_volunteer_type == 'Program':
            return HttpResponse('<script language="JavaScript"> location.href="http://127.0.0.1:8000/login/volunteer_program?id=' + str(item) + '" </script>')

        elif saving.person_volunteer_type == 'Admin"]':
            return HttpResponse('<script language="JavaScript"> alert("You have sucessful created a new Volunteer"); location.href="http://127.0.0.1:8000/login/clock_in/" </script>')

        elif saving.person_volunteer_type == 'Committee':
            return HttpResponse('<script language="JavaScript"> alert("You have sucessful created a new Volunteer"); location.href="http://127.0.0.1:8000/login/clock_in/" </script>')

else:
    form = VolunteerForm()

return render(request, 'loginPortal/volunteer_form.html', {'form' : form})

the on handling the 2nd form: 在处理第二种形式时:

def volunteer_program(request):
if request.method == 'POST':
    form = VolunteerProgramForm(request.POST)
    if form.is_valid():
        saving = form.save(commit=False)
        saving.pvolunteer_personid_id = form.cleaned_data.get('id_pvolunteer_personid_id')
        saving.save()
        return HttpResponse('<script language="JavaScript"> alert("You have sucessful created a new Volunteer"); location.href="http://127.0.0.1:8000/login/clock_in/" </script>')

elif request.method =='GET':
    item = request.GET['id']
    form = VolunteerProgramForm()

    return render(request, 'loginPortal/volunteer_program.html', {'form' : form , 'item' : item })

else:
    form = VolunteerProgramForm()

return render(request, 'loginPortal/volunteer_program.html', {'form' : form })

Generic answer, asuming that your second model has a ForeignKey to the first model. 通用答案,假设您的第二个模型具有第一个模型的ForeignKey。 Do this in your view's post: 在您的视图中执行此操作:

1) Validate the first form, if it won't validate, return with errors. 1)验证第一个表格,如果不通过验证,则返回错误。

2) Validate the second form, if it won't validate, return with errors. 2)验证第二种形式,如果无法通过验证,则返回错误。

3) If both validate, save the instance from the first form: 3)如果两个都验证,请从第一种形式保存实例:

first_instance = first_form.save()

4) Save the instance from the second form without commiting changes to database: 4)保存第二种形式的实例,而无需提交对数据库的更改:

second_instance = second_form.save(commit=False)

5) Update the foreign key on the second model instance: 5)在第二个模型实例上更新外键:

second_instance.fk_to_first_model = first_instance

6) Save the second instance to database 6)将第二个实例保存到数据库

second_instance.save()

7) Return success! 7)成功返回!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM