from django.contrib import admin from django.forms.widgets import SelectMultiple from django.http import HttpResponse from django.utils.html import escape, escapejs #from the_module_where_you_stored_it import RelatedFieldWidgetWrapper class RelatedWidgetWrapperAdmin(admin.ModelAdmin): def formfield_for_dbfield(self, db_field, **kwargs): formfield = super(cls, self).formfield_for_dbfield(db_field, **kwargs) if (formfield and isinstance(formfield.widget, admin.widgets.RelatedFieldWidgetWrapper) and not isinstance(formfield.widget.widget, SelectMultiple)): request = kwargs.pop('request', None) related_modeladmin = self.admin_site._registry.get(db_field.rel.to) can_change_related = bool(related_modeladmin and related_modeladmin.has_change_permission(request)) can_delete_related = bool(related_modeladmin and related_modeladmin.has_delete_permission(request)) widget = RelatedFieldWidgetWrapper.from_contrib_wrapper(formfield.widget, can_change_related, can_delete_related) formfield.widget = widget return formfield def response_change(self, request, obj): if '_popup' in request.REQUEST: pk_value = obj._get_pk_val() return HttpResponse('' % \ # escape() calls force_unicode. (escape(pk_value), escapejs(obj))) else: return super(cls, self).response_change(request, obj)