在Django中获取当前用户登录信号 [英] Get current user log in signal in Django
问题描述
我只是在Django中使用管理站点。我有2个Django信号(pre_save和post_save)。我想拥有当前用户的用户名。我该怎么办?似乎我无法发送请求,或者我听不懂。
I am just using the admin site in Django. I have 2 Django signals (pre_save and post_save). I would like to have the username of the current user. How would I do that? It does not seem I can send a request Or I did not understand it.
谢谢
推荐答案
如果您使用的是管理网站,为什么不使用自定义模型admin
If you are using the admin site why not use a custom model admin
class MyModelAdmin( admin.ModelAdmin ):
def save_model( self, request, obj, form, change ):
#pre save stuff here
obj.save()
#post save stuff here
admin.site.register( MyModel, MyModelAdmin )
信号是每次保存对象时都会触发的信号,而不管它是由管理员完成的还是与请求无关并且实际上不是执行请求的适当位置的某个过程动作
A signal is something that is fired every time the object is saved regardless of if it is being done by the admin or some process that isn't tied to a request and isn't really an appropriate place to be doing request based actions
这篇关于在Django中获取当前用户登录信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!