How can I assign different platform ID to multiple candidates before saving it to the database?

I have a formset that takes in 7 candidates and each candidate should have a platform id. When I try to assign a platform to a candidate in a for loop, it only assigns the first platform ID to all candidates.

here is my view fuction.

def addcandidate(request):
    party = PartyList.objects.all()

    partylist_form = PartylistForm()

    PlatformSet = formset_factory( PlatformForm,extra=7)
    candidateFormSet = formset_factory(CandidateProfileForm,extra=7)

    form1 = PlatformSet(prefix='platform')
    form2 = candidateFormSet(prefix='candidate')

  
    if request.method == 'POST':
        partylist_form = PartylistForm(request.POST)

        form1 = PlatformSet(request.POST, prefix='platform')
        form2 = candidateFormSet(request.POST,request.FILES,prefix='candidate')

        if form1.is_valid() and form2.is_valid() and partylist_form.is_valid():
            
            try:
                partylist =  PartyList.objects.get(partylist_name=request.POST['partylist_name'])
            except ObjectDoesNotExist:
                partylist = partylist_form.save()
            for f1 in form1:
                p1 = f1.cleaned_data['platform']
                p2 = f1.cleaned_data['platform2']
                p3 = f1.cleaned_data['platform3']
                try:
                    Platform.objects.get(candidate_platform=p1)
                except ObjectDoesNotExist:
                    platform = Platform(candidate_platform=p1,candidate_platform2=p2,candidate_platform3=p3)
                    platform.save()

                for count,f2 in enumerate(form2):
                            name = f2.cleaned_data['name']
                            img = f2.cleaned_data['Candidate_Img']
                            try:
                                CandidateProfile.objects.get(name=name)
                                            
                            except ObjectDoesNotExist:
                                candidate =  CandidateProfile(Candidate_Img=img,name=name)
                                candidate.save()
                                candidate.platform = platform
                              
                                candidate.partylist = partylist
                                if count == 0:
                                    candidate.position = Position.objects.get(id=1)
                                elif count == 1:
                                    candidate.position = Position.objects.get(id=2)
                                elif count == 2:
                                    candidate.position = Position.objects.get(id=3)
                                elif count == 3:
                                    candidate.position = Position.objects.get(id=4)
                                elif count == 4:
                                    candidate.position = Position.objects.get(id=5)
                                elif count == 5:
                                    candidate.position = Position.objects.get(id=6)
                                elif count == 6:
                                    candidate.position = Position.objects.get(id=7)
                                candidate.save()

                                 
            return redirect(request.META['HTTP_REFERER'])
        else:

            print(form1.errors)
            print(form2.errors)
            print(form1.non_form_errors)
            print(form2.non_form_errors)

            
    context = {'form1':form1,'form2':form2,'party':party}
    return render(request,"accounts/admindashboard.html", context)

I have tried saving it after assigning a platform ID but the result was the same. only the fist platform is being assigned to all candidates.

Back to Top