知道信号是否从 django admin 触发

Know if signal is triggered from django admin

我只需要在模型从 django admin 保存时发送邮件,我尝试使用 sender.user.is_superuser 但我找不到具体方法

不可能:

Accessing the user's request in a post_save signal

"Can't be done. The current user is only available via the request, which is not available when using purely model functionality. Access the user in the view somehow."

"Django's model signals are intended to notify other system components about events associated with instances and their respected data"

可能的解决方案:

你不能用信号做到这一点,但你可以用你的 ModelAdminsave_model() method

例如:

from django.contrib import admin
from django.core.mail import send_mail

class MyModelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        super().save_model(request, obj, form, change)
        send_mail(...)