ModelAdmin 的重写方法

发布于 2024-12-05 07:37:16 字数 1036 浏览 0 评论 0原文

我正在开发一个 Django 项目,每当管理员在管理控制台 (CRUD) 中执行某些操作时,就会通知一组人。有人向我指出 ModelAdmin 上的三个方法,称为 log_addition、log_created 和 log_deleted,它们将所有必要的信息保存到一个名为“django_admin_log”的特殊数据库中。

我将以下代码放入我的 admin.py 中:

class ModelAdmin(admin.ModelAdmin):
    def log_addition(self, request, object):
      subject = 'admin test of creation'
      message = 'admin creation detected'
      from_addr = '[email protected]'
      recipient_list = ('[email protected]',)
      send_mail(subject, message, from_addr, recipient_list)

      return super(ModelAdmin, self).log_addition( *args, **kwargs )

但是,当我创建新用户时,此代码会被忽略。许多帖子实际上建议创建一个不同的类名(MyModelAdmin),我不完全确定为什么 - 重点是覆盖现有模型。我尝试过,但结果相同。有人能指出我正确的方向吗?究竟如何重写现有类的方法并赋予它一些额外的功能? 谢谢你! Luka

编辑:我想通了,似乎我必须取消注册并重新注册用户才能使我的更改生效。

I am working on a Django project where any time an admin does something in the admin console (CRUD), a set of people gets notified. I was pointed to three methods on ModelAdmin called log_addition, log_created and log_deleted which save all the necessary info into a special database called "django_admin_log".

I placed the following code into my admin.py:

class ModelAdmin(admin.ModelAdmin):
    def log_addition(self, request, object):
      subject = 'admin test of creation'
      message = 'admin creation detected'
      from_addr = '[email protected]'
      recipient_list = ('[email protected]',)
      send_mail(subject, message, from_addr, recipient_list)

      return super(ModelAdmin, self).log_addition( *args, **kwargs )

This code however, gets ignored when I create new users. Many posts actually recommend to create a different class name (MyModelAdmin) and I am not entirely sure why - the point is to override the existing model. I tried it, but with the same result. Can anyone point me in the right direction please? How exactly do you override a method of an existing class and give it some extra functionality?
Thank you!
Luka

EDIT: I figured it out, it seems that I had to unregister and re-register User for my change to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还不是爱你 2024-12-12 07:37:16

删除末尾的回车

如果这不起作用,您可以将代码放入名为 add_view 的函数中:

class ModelAdmin(admin.ModelAdmin):
    add_view(self, request):
        ...
        super(ModelAdmin, self).add_view( *args, **kwargs)

可以覆盖此函数以向管理员视图添加功能。如果您查看管理代码:

https:// /code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L923

你会看到你尝试过的功能overwrite 是从 add_view 函数中调用的,因此您也可以将代码放在这里

remove the return at the end.

If that doesn't work, you can instead put the code in a function called add_view:

class ModelAdmin(admin.ModelAdmin):
    add_view(self, request):
        ...
        super(ModelAdmin, self).add_view( *args, **kwargs)

This function can be overwritten to add functionality to the admin's view. If you look at the admin code:

https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L923

you will see that the function you have tried to overwrite is called from within the add_view function so you could equally put the code here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文