Search code examples
djangodjango-modelsdjango-signals

How to get currently logged in user in django model on post_save signals?


i want to store the information who created the object ???

trying something like this ..

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) 

Solution

  • To complement Altaisoft's answer : you have to understand that models are not dependent on HTTP requests and can be created / updated / deleted in a Python script (guess what ./manage.py loaddata do ?), a Python shell, whatever, so there's not necessarily a "logged in user".