0

I'm trying to add Django-notifications to my drf project. I get response when hitting the endpoint:

[
    {
        "recipient": {
            "id": 274,
            "username": "harry",
            "first_name": "Harry",
            "last_name": "Moreno"
        },
        "unread": true,
        "target": null,
        "verb": "invite approved"
    }
]

serializers.py

class GenericNotificationRelatedField(serializers.RelatedField):                                                          
    User = get_user_model()                                                                                               

    def to_representation(self, value):                                                                                   
        if isinstance(value, Invite):                                                                                     
            serializer = InviteSerializer(value)                                                                          
        if isinstance(value, User):                                                                                       
            serializer = UserSerializer(value)                                                                        
        return serializer.data                                                                                            


class NotificationSerializer(serializers.Serializer):                                                                     
    recipient = UserSerializer(read_only=True)                                                                            
    unread = serializers.BooleanField(read_only=True)                                                                     
    target = GenericNotificationRelatedField(read_only=True)  

How do I make the target non-null?

1 Answer 1

3

Turns out the target is null because that is how I created the notification in the model

notify.send(user, recipient=user, verb='you reached level 10')

if I wanted a non-null target I should specify one like

notify.send(user, recipient=user, target=user, verb='you reached level 10')

Note: there is no django view that generates the json in the question. In our urls.py we wire up the route to the notification view from the app.

    path(
      "alerts/",
      views.NotificationViewSet.as_view({"get": "list"}),
      name="notifications",
    ),

see the installation instructions https://github.com/django-notifications/django-notifications#installation

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, would it be possible for you to share the view code that generates the json you posted above please?
added a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.