Wednesday, November 20, 2013

Re: data not saving through a many to many relationship

Update:

So editing AdviceLevel with

def edit_advice(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/user/login/')
    this_advice = request.POST.get('advice_id')
    sp = Specialism.objects.all().filter(advicelevel=this_advice)
    de = CRSDeliveryMethod.objects.all().filter(advicelevel=this_advice)
    lo = Location.objects.all().filter(advicelevel=this_advice)
    help = OrganisationHelpTexts.objects.all()
    advice = request.POST.get('advice')
 #   adv = Advice.objects.get(title=advice)
 #   id_adv = adv.id
    this_org = request.session['this_org']
    this = Organisation.objects.get(pk=this_org)
    id_org = this.id
    thisadvlvl = AdviceLevel.objects.get(pk=this_advice)
    level_1 = thisadvlvl.level_1
    level_2 = thisadvlvl.level_2
    level_3 = thisadvlvl.level_3
    level_4 = thisadvlvl.level_4

    advkey = AdviceLevelKey.objects.all()
    if request.POST:
    #    raise NameError(request.POST)
        if 'adviceid' in request.POST:
            return HttpResponseRedirect('/directory/delete_advice/')
        if 'editform' in request.POST:
            editform = EditAdviceLevelForm(request.POST, instance=thisadvlvl)
            if editform.is_valid():
                error = ""
                level_1 = editform.cleaned_data['level_1']
                level_2 = editform.cleaned_data['level_2']
                level_3 = editform.cleaned_data['level_3']
                level_4 = editform.cleaned_data['level_4']
                service_restriction = editform.cleaned_data['service_restriction']
                service_supervisor = editform.cleaned_data['service_supervisor']
                specialism = editform.cleaned_data['specialism']
                delivery_method = editform.cleaned_data['delivery_method']
                face_to_face_locations = editform.cleaned_data['face_to_face_locations']
                if (level_3 == 0 and level_4 == 1) or (level_2 == 0 and (level_3 == 1 or level_4 == 1)) or (level_1 == 0 and (level_2 == 1 or level_3 == 1 or level_4 == 1)):
                    error = "That isn't a possible combination, the tiers are progressive"
                    editform = EditAdviceLevelForm(request.POST, instance=thisadvlvl)
                    return render_to_response('directory/edit_advice.html', {'thisadvlvl': thisadvlvl, 'help': help, 'sp': sp, 'lo': lo, 'de': de, 'advice': advice, 'advkey': advkey, 'editform': editform, 'level_1': level_1, 'level_2': level_2, 'level_3': level_3, 'level_4': level_4, 'this_advice': this_advice, 'error': error, },context_instance=RequestContext(request))
                editform.save()
                return HttpResponseRedirect('/directory/do_advice/')

        else:
            editform = EditAdviceLevelForm(request.POST, instance=thisadvlvl)
    else:
        editform = EditAdviceLevelForm(instance=thisadvlvl)
    return render_to_response('directory/edit_advice.html', {'thisadvlvl': thisadvlvl, 'help': help, 'sp': sp, 'lo': lo, 'de': de, 'advice': advice, 'advkey': advkey, 'editform': editform, 'level_1': level_1, 'level_2': level_2, 'level_3': level_3, 'level_4': level_4, 'this_advice': this_advice, },context_instance=RequestContext(request))

works both adding or deleting a many to many related field so the difference is in the save methods
editform.save() in this working case and
af = adviceform.save(commit=False)
af.organisation = organisation
af.save()
which doesn't

Now I deliberately used commit = False because I wanted to prepopulate the organisation rather than have the user select(again) the organisation so I therefore had to add the value of organisation to the form save hence using commit=False, unless someone can spot an error somewhere that is causing the problem or knows of a differnt way of accomplishing what commit does.  Cannot just save the form as adviceform.save() because organisation MUST have a value see the Organisation model.
Any ideas?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/97de130b-1f4f-492b-938e-e18d28dfd5da%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment