serializers.py 973 Bytes
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
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 37


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

        fields = ('key', 'name')