question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serializing the django-notifications Model

See original GitHub issue

I’m trying to serialize the model included in django-notifications package with DRF (for a RESTFUL API)

But i’m facing a problem. When I acces the model Notification inside notifications\models.py and try to make a ModelSerializer of it, then raise this error:

NotificationQuerySet' object has no attribute 'recipient'.

My ModelSerializer:

class NotificationSerializer(serializers.ModelSerializer):

    class Meta:
        model = Notification
        fields = ('description', 'recipient',)

Any tip on this? I don’t understand why is trying to accesing NotificationQuerySet instead of Notification model…

Thanks in andvance.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
mwcbrentcommented, May 30, 2017

I know this is a really old question but I solved this recently and wanted to share my solution. My solution took advantage of the generic foreign key support in django rest framework. See below:

class GenericNotificationRelatedField(serializers.RelatedField):

    def to_representation(self, value):
        if isinstance(value, Foo):
            serializer = FooSerializer(value)
        if isinstance(value, Bar):
            serializer = BarSerializer(value)

        return serializer.data


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

See here - http://www.django-rest-framework.org/api-guide/relations/#generic-relationships

In this example the target object can be of type Foo or Bar and the appropriate serializer will be used.

2reactions
AlvaroLQueirozcommented, Jun 27, 2017

Added the exemple in README file. Thanks @DaWy .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializing NotificationQuerySet from django-notifications-hq ...
So, i'm trying to add to my API made with DRF (Django REST Framework) the notifications Model ...
Read more >
Django Notifications - velog
Serializing the django -notifications Model · Inheriting the AbstractNotification model for custom fields.
Read more >
Models — Django Notify 0.1 documentation
This model is pretty-much a replica of django-notifications 's model. The newly added fields just adds a feature to allow anonymous actors ,...
Read more >
Serializing Django objects
Django's serialization framework provides a mechanism for “translating” Django models into other formats. Usually these other formats will be text-based and ...
Read more >
Serializer relations - Django REST framework
Relational fields are used to represent model relationships. ... If AlbumSerializer is used to serialize a fairly large queryset with many=True then it ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found