serializers.py 989 Bytes
Newer Older
1 2
from __future__ import absolute_import

3
from rest_framework.relations import ManyRelatedField, PrimaryKeyRelatedField
4 5
from rest_framework.serializers import ModelSerializer

6
from pushnotifications.models import Device, Category
7 8 9


class DeviceSerializer(ModelSerializer):
10 11 12 13 14 15 16 17

    receive_category = ManyRelatedField(
        allow_empty=True,
        child_relation=PrimaryKeyRelatedField(allow_empty=True,
                                              queryset=Category.objects.all(),
                                              required=False)
    )

18 19 20
    class Meta:
        model = Device

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

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


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

        fields = ('key', 'name')