python - 在 post_save 信号中访问用户的请求

标签 python django signals

我在我的项目中完成了以下 post_save 信号。

from django.db.models.signals import post_save
from django.contrib.auth.models import User

# CORE - SIGNALS
# Core Signals will operate based on post

def after_save_handler_attr_audit_obj(sender, **kwargs):
    print User.get_profile()

    if hasattr(kwargs['instance'], 'audit_obj'):
        if kwargs['created']:
            kwargs['instance'].audit_obj.create(operation="INSERT", operation_by=**USER.ID**).save()
        else:
            kwargs['instance'].audit_obj.create(operation="UPDATE").save()


# Connect the handler with the post save signal - Django 1.2
post_save.connect(after_save_handler_attr_audit_obj, dispatch_uid="core.models.audit.new")

operation_by 列,我想获取 user_id 并存储它。知道怎么做吗?

最佳答案

做不到。当前用户只能通过请求获得,这在使用纯模型功能时不可用。以某种方式访问​​ View 中的用户。

关于python - 在 post_save 信号中访问用户的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4716330/

相关文章:

c++ - 使用信号处理程序处理从 fifo 接收的数据

c - 信号中断时释放共享内存

python - 从Pool.async_map返回第一个非零结果

django - NextJS 使用 Django API - 如何选择最佳模式

python - 测试发现: list all tests in a format suitable for copy/paste

python - 在内存中运行 Django MySQL 测试

linux - 检测信号发送者(linux,ptrace)

python - 搜索多个字符串并打印出匹配项

python - Skype4Py 附加功能第二次不起作用

python - 有没有办法在Python中从右到左累加?