首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在Django admin中隐藏删除后的对象?

在Django admin中隐藏删除后的对象可以通过重写ModelAdmin类的delete_view方法来实现。具体步骤如下:

  1. 创建一个admin.py文件,并导入相关模块:
代码语言:txt
复制
from django.contrib import admin
from django.contrib.admin.utils import unquote
from django.contrib.admin.views.main import IS_POPUP_VAR
from django.contrib.admin.options import get_deleted_objects
from django.contrib.admin.utils import model_ngettext
from django.contrib.admin import helpers
from django.contrib.admin.utils import quote
from django.http import HttpResponseRedirect
from django.template.response import TemplateResponse
from django.utils.encoding import force_text
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _
  1. 创建一个自定义的ModelAdmin类,继承自admin.ModelAdmin,并重写delete_view方法:
代码语言:txt
复制
class CustomModelAdmin(admin.ModelAdmin):
    def delete_view(self, request, object_id, extra_context=None):
        """
        The 'delete' admin view for this model.
        """
        model = self.model
        opts = model._meta

        obj = self.get_object(request, unquote(object_id))

        if not self.has_delete_permission(request, obj):
            raise PermissionDenied

        using = router.db_for_write(model)

        if request.method == 'POST':
            if obj is None:
                raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {
                    'name': force_text(opts.verbose_name),
                    'key': escape(object_id),
                })

            # Populate deleted_objects, a data structure of all related objects that
            # will also be deleted.
            deleted_objects, model_count, perms_needed, protected = get_deleted_objects(
                [obj], opts, request.user, self.admin_site, using)

            if request.POST.get('post'):
                if perms_needed:
                    raise PermissionDenied(_("You do not have permission to delete this %(name)s.") % {
                        'name': force_text(opts.verbose_name),
                    })
                obj_display = force_text(obj)
                self.log_deletion(request, obj, obj_display)
                self.delete_model(request, obj)

                return self.response_delete(request, obj_display, obj)

            if perms_needed or protected:
                title = _("Cannot delete %(name)s") % {"name": force_text(opts.verbose_name)}
            else:
                title = _("Are you sure?")

            context = {
                **self.admin_site.each_context(request),
                'title': title,
                'object_name': force_text(opts.verbose_name),
                'object': obj,
                'deleted_objects': deleted_objects,
                'model_count': dict(model_count).items(),
                'perms_lacking': perms_needed,
                'protected': protected,
                'opts': opts,
                'root_path': self.admin_site.root_path,
                'app_label': opts.app_label,
                'preserved_filters': self.get_preserved_filters(request),
                'is_popup': (IS_POPUP_VAR in request.POST or
                             IS_POPUP_VAR in request.GET),
                'obj': obj,
            }

            # Display the confirmation page
            return TemplateResponse(request, self.delete_confirmation_template or [
                "admin/%s/%s/delete_confirmation.html" % (opts.app_label, opts.model_name),
                "admin/%s/delete_confirmation.html" % opts.app_label,
                "admin/delete_confirmation.html"
            ], context)

        context = {
            **self.admin_site.each_context(request),
            'title': _("Are you sure?"),
            'object_name': force_text(opts.verbose_name),
            'object': obj,
            'opts': opts,
            'root_path': self.admin_site.root_path,
            'app_label': opts.app_label,
            'preserved_filters': self.get_preserved_filters(request),
            'is_popup': (IS_POPUP_VAR in request.POST or
                         IS_POPUP_VAR in request.GET),
            'obj': obj,
        }

        # Display the confirmation page
        return TemplateResponse(request, self.delete_confirmation_template or [
            "admin/%s/%s/delete_confirmation.html" % (opts.app_label, opts.model_name),
            "admin/%s/delete_confirmation.html" % opts.app_label,
            "admin/delete_confirmation.html"
        ], context)
  1. 在admin.py文件中注册你的ModelAdmin类:
代码语言:txt
复制
from django.contrib import admin
from .models import YourModel

admin.site.register(YourModel, CustomModelAdmin)

通过以上步骤,你可以在Django admin中隐藏删除后的对象。当你删除一个对象时,它将不再显示在列表中,但仍然存在于数据库中。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • 领券