如何在post_save信号上获取django模型中当前登录的用户?

时间:2013-11-13 10:55:30

标签: django django-models django-signals

我想存储创建对象的信息???

尝试这样的事情......

def event_submitted(sender,instance,created,**args):
if created:
        content_type = ContentType.objects.get(app_label='activity', model='event')
        ModerationItem.objects.create(submitted_by= ?????,    # what to put here

                                      submitted_remarks=instance.remarks,
                                      activity_content_type=content_type,
                                      activity_object_id=instance.id,
                                      )


signals.post_save.connect(event_submitted , sender = Event) 

2 个答案:

答案 0 :(得分:2)

为什么不在视图中填写此数据?

def my_view(request):
    my_instance = Event.objects.get(pk=...)
    # Fill your instance data, for example, from a submitted form
    my_instance.submitted_by = request.user
    my_instance.save()

答案 1 :(得分:0)

补充Altaisoft的答案:您必须了解模型不依赖于HTTP请求,可以在Python脚本中创建/更新/删除(猜猜./manage.py loaddata做什么?),Python shell,无论如何,所以不一定是“登录用户”。