I've implemented some code already to de-reference (SET_NULL) on
delete, in a view which first presents a list of objects which will be
deleted and a list of objects which will be de-referenced. It might
not be the most efficient approach, but it could be a starting point.

def _clear(instance):
        """
        recursively clear related models before deleting `instance`.
        """
        cleared = []
        deleted = []

        for related_set in (getattr(instance, elem) for elem in dir(instance)
if re.search(r'_set$', elem)):
                if hasattr(related_set, 'clear'):
                        cleared.extend(elem for elem in related_set.all())
                        related_set.clear()
                elif hasattr(related_set, 'all'):
                        for related_instance in related_set.all():
                                _cleared, _deleted = _clear(related_instance)
                                deleted.extend(_deleted)

        return cleared, ((instance, deleted), )

@user_is_authenticated()
@request_passes_test(lambda r: r.user.is_superuser)
@transaction.commit_manually
def delete(request, app_label, model_name, pk, template='base/
delete.html', next='/base/'):
        try:
                model = get_model(app_label, model_name)
                instance = model.objects.get(pk=pk)
        except model.DoesNotExist:
                raise Http404

        if request.method == 'POST':
                alert = '<b>%s</b> successfully deleted.' % instance

                _clear(instance)
                instance.delete()
                transaction.commit()

                # set alert and redirect.
                context = RequestContext(request, {
                })
                request.session['alert'] = alert
                request.session['alert_class'] = 'good'
                return HttpResponseRedirect(context['base']['next'] or next)

        cleared, deleted = _clear(instance)
        transaction.rollback()

        # set context and render template.
        context = RequestContext(request, {
                'cleared': cleared,
                'deleted': deleted,
        })
        return render_to_response(template, context)

One other thing that you might need to consider is how any change here
will affect post_delete and post_save triggers. Will they still fire
when a supported database executes a cascading delete or SET_NULL, or
will they only fire when a delete/update is implemented through the
Django ORM?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to