mirror of
https://github.com/Hopiu/django-notifications.git
synced 2026-05-10 14:24:44 +00:00
Serializing the django-notifications Model exemple
This commit is contained in:
parent
761d289133
commit
877713681e
1 changed files with 25 additions and 0 deletions
25
README.rst
25
README.rst
|
|
@ -373,6 +373,31 @@ Testing the live-updater
|
|||
4. Browse to `yourserverip/test/`
|
||||
5. Click 'Make a notification' and a new notification should appear in the list in 5-10 seconds.
|
||||
|
||||
Serializing the django-notifications Model
|
||||
------------------------------------------
|
||||
|
||||
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. (Thanks to @DaWy)
|
||||
|
||||
```python
|
||||
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)
|
||||
```
|
||||
|
||||
Notes
|
||||
=====
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue