serializers.py 1.12 KB
Newer Older
1
from rest_framework.relations import ManyRelatedField, PrimaryKeyRelatedField
2 3
from rest_framework.serializers import ModelSerializer

4
from pushnotifications.models import Device, Category, Message
5 6 7


class DeviceSerializer(ModelSerializer):
8 9 10

    receive_category = ManyRelatedField(
        allow_empty=True,
11
        required=False,
12 13 14 15 16
        child_relation=PrimaryKeyRelatedField(allow_empty=True,
                                              queryset=Category.objects.all(),
                                              required=False)
    )

17 18 19
    class Meta:
        model = Device

20 21 22 23 24 25 26 27
        fields = (
            'pk',
            'registration_id',
            'active',
            'date_created',
            'type',
            'receive_category'
        )
28 29 30
        read_only_fields = ('date_created',)

        extra_kwargs = {'active': {'default': True}}
31 32 33 34 35 36


class CategorySerializer(ModelSerializer):
    class Meta:
        model = Category

37
        fields = ('key', 'name', 'description')
38 39 40 41 42 43 44


class MessageSerializer(ModelSerializer):
    class Meta:
        model = Message

        fields = ('pk', 'title', 'body', 'url', 'category')