Irshadmi4
Irshadmi4

Reputation: 500

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) 

Upvotes: 3

Views: 2098

Answers (2)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

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".

Upvotes: 0

Anatoly Scherbakov
Anatoly Scherbakov

Reputation: 1762

Why not fill this data in your view?

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()

Upvotes: 3

Related Questions