theme template superusuario personalizar password create crear django django-admin

template - django-admin command



La acción de administrador predeterminada "eliminar seleccionados" en Django (8)

Como alternativa a la solución de Googol, y al esperar a que delete_model () se implemente en la versión actual de Django , sugiero el siguiente código.

Deshabilita la acción de eliminación predeterminada solo para el AdminForm actual.

class FlowAdmin(admin.ModelAdmin): actions = [''delete_model''] def get_actions(self, request): actions = super(MyModelAdmin, self).get_actions(request) del actions[''delete_selected''] return actions def delete_model(self, request, obj): for o in obj.all(): o.delete() delete_model.short_description = ''Delete flow'' admin.site.register(Flow, FlowAdmin)

¿Cómo puedo eliminar o cambiar el nombre detallado de la acción de administrador predeterminada "eliminar el elemento X seleccionado" en el panel de administración de Django?


Para cambiar globalmente la descripción de borrado_seleccionado de , la answer parece ser la mejor.

Sin embargo, para cambiar la descripción breve en el administrador para un modelo único, creo que esta alternativa a la answer Stéphane es mejor:

def get_actions(self, request): actions = super(MyModelAdmin, self).get_actions(request) actions[''delete_selected''][0].short_description = "Delete Selected" return actions


Para reemplazar delete_selected hago lo siguiente:

Copie la función delete_selected de contrib/admin/actions.py a su admin.py y admin.py nombre. También copie la plantilla contrib/admin/templates/delete_selected_confirmation.html en el directorio de su plantilla y cambie su nombre. El mío se parece a esto:

def reservation_bulk_delete(modeladmin, request, queryset): """ Default action which deletes the selected objects. This action first displays a confirmation page whichs shows all the deleteable objects, or, if the user has no permission one of the related childs (foreignkeys), a "permission denied" message. Next, it delets all selected objects and redirects back to the change list. """ opts = modeladmin.model._meta app_label = opts.app_label # Check that the user has delete permission for the actual model if not modeladmin.has_delete_permission(request): raise PermissionDenied # Populate deletable_objects, a data structure of all related objects that # will also be deleted. # deletable_objects must be a list if we want to use ''|unordered_list'' in the template deletable_objects = [] perms_needed = set() i = 0 for obj in queryset: deletable_objects.append([mark_safe(u''%s: <a href="%s/">%s</a>'' % (escape(force_unicode(capfirst(opts.verbose_name))), obj.pk, escape(obj))), []]) get_deleted_objects(deletable_objects[i], perms_needed, request.user, obj, opts, 1, modeladmin.admin_site, levels_to_root=2) i=i+1 # The user has already confirmed the deletion. # Do the deletion and return a None to display the change list view again. if request.POST.get(''post''): if perms_needed: raise PermissionDenied n = queryset.count() if n: for obj in queryset: obj_display = force_unicode(obj) obj.delete() modeladmin.log_deletion(request, obj, obj_display) #queryset.delete() modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % { "count": n, "items": model_ngettext(modeladmin.opts, n) }) # Return None to display the change list page again. return None context = { "title": _("Are you sure?"), "object_name": force_unicode(opts.verbose_name), "deletable_objects": deletable_objects, ''queryset'': queryset, "perms_lacking": perms_needed, "opts": opts, "root_path": modeladmin.admin_site.root_path, "app_label": app_label, ''action_checkbox_name'': helpers.ACTION_CHECKBOX_NAME, } # Display the confirmation page return render_to_response(modeladmin.delete_confirmation_template or [ "admin/%s/%s/reservation_bulk_delete_confirmation.html" % (app_label, opts.object_name.lower()), "admin/%s/reservation_bulk_delete_confirmation.html" % app_label, "admin/reservation_bulk_delete_confirmation.html" ], context, context_instance=template.RequestContext(request))

Como puedes ver comento

queryset.delete()

y más bien usar:

obj.delete()

Aún no es óptimo: debe aplicar algo a todo el conjunto de consultas para obtener un mejor rendimiento.

En admin.py deshabilito la acción predeterminada delete_selected para todo el sitio de administración:

admin.site.disable_action(''delete_selected'')

En su lugar, uso mi propia función cuando sea necesario:

class ReservationAdmin(admin.ModelAdmin): actions = [reservation_bulk_delete, ]

En mi modelo defino la función delete() :

class Reservation(models.Model): def delete(self): self.status_server = RESERVATION_STATUS_DELETED self.save()


Puede desactivar la acción para que no aparezca con este código.

from django.contrib import admin admin.site.disable_action(''delete_selected'')

Si lo elige, puede restaurarlo en modelos individuales con esto:

class FooAdmin(admin.ModelAdmin): actions = [''my_action'', ''my_other_action'', admin.actions.delete_selected]



No estoy seguro de que este tipo de parches de mono sea una buena idea, pero poner esto en uno de mis admin.py funciona para mí:

from django.contrib.admin.actions import delete_selected delete_selected.short_description = u''How/'s this for a name?''

Esto cambiará el nombre detallado para todos los sitios de administración. Si desea cambiarlo solo para el administrador de un modelo en particular, creo que tendrá que escribir una acción de administrador personalizada .

Probado con la versión 1.1 de Django:

>>> import django >>> django.VERSION (1, 1, 0, ''beta'', 1)


class FooAdmin(sites.AdminSite): not_deleted = [''value1'', ''value2''] actions = [''delete_selected_values''] def delete_selected_values(self, request, queryset): # my custom logic exist = queryset.filter(value__in=self.not_deleted).exists() if exist: error_message = "Error" self.message_user(request, error_message, level=messages.ERROR) else: delete_action = super().get_action(''delete_selected'')[0] return delete_action(self, request, queryset) delete_selected_values.short_description = ''delete selected'' admin.site.register(Foo, FooAdmin)


from django.contrib.admin import sites from django.contrib.admin.actions import delete_selected class AdminSite(sites.AdminSite): """ Represents the administration, where only authorized users have access. """ def __init__(self, *args, **kwargs): super(AdminSite, self).__init__(*args, **kwargs) self.disable_action(''delete_selected'') self.add_action(self._delete_selected, ''delete_selected'') @staticmethod def _delete_selected(modeladmin, request, queryset): _delete_qs = queryset.delete def delete(): for obj in queryset: modeladmin.delete_model(request, obj) _delete_qs() queryset.delete = delete return delete_selected(modeladmin, request, queryset)