def merge_selected(modeladmin,request,queryset): #This is an admin/ import copy model = queryset.model model_name = model._meta.object_name return_url = "." list_display = copy.deepcopy(modeladmin.list_display) ids = [] if '_selected_action' in request.POST: #List of PK's of the selected models ids = request.POST.getlist('_selected_action') if 'id' in request.GET: #This is passed in for specific merge links. This id comes from the linking model (Consumer, IR, Contact, ...) id = request.GET.get('id') ids.append(id) try: queryset = queryset | model.objects.filter(pk=id) except AssertionError: queryset = model.objects.filter(pk__in=ids) return_url = model.objects.get(pk=id).get_absolute_url() or "." if 'return_url' in request.POST: return_url = request.POST['return_url'] if 'master' in request.POST: master = model.objects.get(id=request.POST['master']) queryset = model.objects.filter(pk__in=ids) for q in queryset.exclude(pk=master.pk): model_merge(master,q) messages.success(request,"All " + model_name + " records have been merged into the selected " + model_name + ".") return HttpResponseRedirect(return_url) #Build the display_table... This is just for the template. #---------------------------------------- display_table = [] try: list_display.remove('action_checkbox') except ValueError: pass titles = [] for ld in list_display: if hasattr(ld,'short_description'): titles.append(strings.pretty(ld.short_description)) elif hasattr(ld,'func_name'): titles.append(strings.pretty(ld.func_name)) elif ld == "__str__": titles.append(model_name) else: titles.append(ld) display_table.append(titles) for q in queryset: row = [] for ld in list_display: if callable(ld): row.append(mark_safe(ld(q))) elif ld == "__str__": row.append(q) else: row.append(mark_safe(getattr(q,ld))) display_table.append(row) display_table[-1:][0].insert(0,q.pk) #---------------------------------------- return render_to_response('merge_preview.html',{'queryset': queryset, 'model': model, 'return_url':return_url,\ 'display_table':display_table, 'ids': ids}, context_instance=RequestContext(request)) merge_selected.short_description = "Merge selected records" #==========html=============== {% extends "section_base.html" %} {% load form_tags %} {% block title %}Confirm Merger{% endblock %} {% block breadcrumbs %}{{ block.super }} Merge Preview{% endblock %} {% block body %}
Please select which record you would like to keep. The selected record's information will be used, and all related information from the other records will be moved to it. The other records will be deleted. If you are unsure how to use this, please contact your supervisor or COMS Support. Changes made here cannot be reversed.