5. Api key authentication¶
Allows you to manage access to the FCM api using one of the available
tastypie authentication methods - ApiKeyAuthentication.
I strongly recommend see django-tastypie Authentication docs.
Adding authentication requires djangorestframework added to your INSTALLED_APPS in the
INSTALLED_APPS = [ ... 'fcm', 'rest_framework', ]
5.1. Adding user field¶
You need to extend Device model and add user field. (See Extending device model)
# your_app/models.py from django.conf import settings from django.db import models from fcm.models import AbstractDevice class MyDevice(AbstractDevice): user = models.ForeignKey(settings.AUTH_USER_MODEL)
Add appropriate path to the
FCM_DEVICE_MODEL = 'your_app.models.MyDevice'
5.2. Serializer class¶
In your application , you can create the serializer, or customize it according the extra field you have included in your Device model.
from rest_framework import serializers from fcm.models import Device class DeviceSerializer(serializers.ModelSerializer): class Meta: model = Device fields = ('dev_id','reg_id','name','is_active')
5.3. View class¶
In your application, you need to create your view either through ModelViewSet or can user or override methods as specified in django-rest-framework documentation.
from rest_framework import viewsets from fcm.models import Device from fcm.serializers import DeviceSerializer class DeviceViewSet(viewsets.ModelViewSet): queryset = Device.objects.all() serializer_class = DeviceSerializer
You need to hook your viewset class up in your
# your_app/urls.py from django.conf.urls import url, include from rest_framework import routers from fcm.views import DeviceViewSet router = routers.DefaultRouter() router.register(r'devices', DeviceViewSet) urlpatterns = [ url(r'^v1/', include(router.urls)) ]
urls.py file in the main URL router:
# urls.py from django.conf.urls import include, url urlpatterns = [ url(r'', include('your_app.urls')), ]