我在我的项目中完成了以下 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/