简体   繁体   中英

Capturing logentry history in post_save signal django

I have written a post_save signal that is like this :

def send_something(sender, instance, **kwargs):
    user = LogEntry.objects.get(
            content_type_id = ContentType.objects.get_for_model(instance).pk,
             object_id      =  instance.pk,
             action_flag    =   1
             ).user

post_save.connect(send_something, sender=ClassName)

But I get a DoesNotExist LogEntry matching query does not exist when I try to add a ClassName object from the admin.

Why does it happen ? Why is the history not available at the time of post_save ? Isn't my object saved yet ?

self.pk似乎是你的问题,尝试用instance.pk替换它

Is this LogEntry being saved somewhere else? Because you're trying to retrieve a log entry after a post save, but those logentries are only created automatically when using the Admin. If you're not using it, chances are that these logs are not being saved. You could use this post_save signal to create those entries first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM