3

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

2 回答 2

3

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()
于 2013-11-13T12:01:17.830 回答
0

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

于 2013-11-13T12:12:01.893 回答