在post_save信号中访问用户的请求 [英] Accessing the user's request in a post_save signal

查看:63
本文介绍了在post_save信号中访问用户的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have done the below post_save signal in my project.

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并将其存储。知道怎么做吗?

The operation_by column, I want to get the user_id and store it. Any idea how can do that?

推荐答案

无法完成。当前用户只能通过请求使用,而在使用纯模型功能时则不可用。以某种方式访问​​视图中的用户。

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.

这篇关于在post_save信号中访问用户的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆