'How to access the actor of a django notification in a usable format?

I am using django-notifications (here is the code) to create notifications for my web app.

I have the issue of whenever I try to access the actor object e.g. via Notification.object.get(id=1).actor I get the following exception:

 ValueError: Field 'id' expected a number but got '<property object at 0x7fc171cc5400>'.

which then causes then exception:

ValueError: invalid literal for int() with base 10: '<property object at 0x7fc171cc5400>'

Here is the code for my signal:

@receiver(post_save, sender=Request)
def notify_owner_of_request(sender, instance, created, **kwargs):
    if created:
        notify.send(
            sender=sender,
            actor = instance.user,
            verb = "requested to join ",
            recipient = instance.owner,
            action_object = instance,
            target = instance.club,
        )

No matter what kind of object or value I make the actor it always has the same error.

To note, I can access action_object, target, verb, and recipient perfectly fine, and the notification does work (there is one made and correctly).

The Notification model from notification-hq has the following attributes:

 actor_content_type = models.ForeignKey(ContentType, related_name='notify_actor', on_delete=models.CASCADE)
actor_object_id = models.CharField(max_length=255)
actor = GenericForeignKey('actor_content_type', 'actor_object_id')

accessing actor_content_type gives me this:

<ContentType: clubs | request>

even though it should be of type User

and accessing actor_object_id gives me this:

'<property object at 0x7fc171cc5400>'

I need to access the actor so I can test it is the correct user and so that it is displayed in the front-end part of the notifications correctly.

Any help would be much appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source