def agent_associations(request, agent_id):
agent = get_object_or_404(EconomicAgent, pk=agent_id)
HasAssociatesFormSet = inlineformset_factory(
EconomicAgent,
AgentAssociation,
fk_name = "has_associate",
form=HasAssociateForm,
extra=3,
)
has_associates_formset = HasAssociatesFormSet(
instance=agent,
queryset=agent.all_has_associates(),
prefix = "has",
data=request.POST or None)
IsAssociatesFormSet = inlineformset_factory(
EconomicAgent,
AgentAssociation,
fk_name = "is_associate",
form=IsAssociateForm,
extra=3,
)
is_associates_formset = IsAssociatesFormSet(
instance=agent,
queryset=agent.all_is_associates(),
prefix = "is",
data=request.POST or None)
if request.method == "POST":
#import pdb; pdb.set_trace()
keep_going = request.POST.get("keep-going")
just_save = request.POST.get("save")
for form in has_associates_formset:
if form.is_valid():
deleteme = form.cleaned_data['DELETE']
if deleteme:
association = form.save(commit=False)
if association.id:
association.delete()
else:
form.save()
for form in is_associates_formset:
if form.is_valid():
deleteme = form.cleaned_data['DELETE']
if deleteme:
association = form.save(commit=False)
if association.id:
association.delete()
else:
form.save()
if just_save:
return HttpResponseRedirect('/%s/%s/'
% ('accounting/agent', agent.id))
elif keep_going:
return HttpResponseRedirect('/%s/%s/'
% ('accounting/agent-associations', agent.id))
return render_to_response("valueaccounting/agent_associations.html", {
"agent": agent,
"has_associates_formset": has_associates_formset,
"is_associates_formset": is_associates_formset,
"help": get_help("associations"),
}, context_instance=RequestContext(request))
评论列表
文章目录