简体   繁体   中英

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.

my Models: User Person Volunteer - Client - Staff

Than I have 3 types of volunteers: Admin; 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. but on the database, only keeps the id for the reference.

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. Do this in your view's post:

1) Validate the first form, if it won't validate, return with errors.

2) Validate the second form, if it won't validate, return with errors.

3) If both validate, save the instance from the first form:

first_instance = first_form.save()

4) Save the instance from the second form without commiting changes to database:

second_instance = second_form.save(commit=False)

5) Update the foreign key on the second model instance:

second_instance.fk_to_first_model = first_instance

6) Save the second instance to database

second_instance.save()

7) Return success!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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