Make the best out ofย ReST..

ReSTful APIs viaย drf

Hi guys ๐Ÿ‘‹, howโ€™re you?

This was my most productive week ever, probably because of my mentors ๐Ÿ™๐Ÿป. Our project started gaining users, yayy.. ๐ŸŽ‰ ๐ŸŽŠ, but so did the problems begin.๐Ÿ˜”

I was so stupid ๐Ÿ˜ž in framing a relationship between two models Repository and Settings. I used a many-one realtion between them, not realizing that they should be one-one mapped. This caused multiple entries and re-entries potentially flooding our server with unused data.

Thanks to Lasse, ๐Ÿ˜ฒ he added the new relation as one-one and weโ€™re all good. We now have a new administration options in gitmate as well. Multiple users can now own a same repository while one of them operates on them. This helps teams collaborate better on themselves. ๐Ÿ˜‰

Also, I finished implementing the IGitt interfaces for GitLab. Hopefully, integrating this into GitMate-2 should be a breeze too, since theyโ€™re built using the same interface.

Django โค๏ธ is really awesome in the way it handles Serializers and Viewsets. Adding model mixins would create a full fledged CRUD compatible ReST API, just like that.

class UserViewSet(
GenericViewSet,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin
):
authentication_classes = (SessionAuthentication,
BasicAuthentication)
permission_classes = (IsAuthenticated,)
serializer_class = UserSerializer
queryset = User.objects.all()

Iโ€™ll try and explain the ViewSets briefly. A ViewSet is basically the backbone of a really good class based APIView for django-rest-framework. Basically we could do anything with ViewSets, but Iโ€™m just gonna focus on model based stuff only(which is likely the most crucial part). We can use ViewSets to simplify operations like create, list, retrieve, update and delete on models. We start over with a GenericViewSet that handles nothing and all mixins as per our requirements.

ListModelMixin, RetrieveModelMixin, CreateModelMixin, UpdateModelMixin and DestroyModelMixin are the basic classes which are to be used to provide all of CRUD. They hold list, retrieve, create, update and destroy methods respectively. Their names speak for themselves, right.

Now speaking of permissions and authentication, authentication_classes and permission_classes properties could be set for the ViewSet according to the needs. Here comes the heart of every ViewSet, the queryset and serializer_classย . The former tells the ViewSet where to look for data and the latter tells it how to present it through the API. A ViewSet basically filters data from queryset with your logic and parses it through the serializer class matching the respective fields. This is basically what a serializer looks like.

from rest_framework import serializers
class UserSerializer(serializers.Serializer):
email = serializers.EmailField()
username = serializers.CharField()
first_name = serialisers.CharField()
last_name = serializers.CharField()

Basically a serializer has fields just like a model which correspond to the type of data they present, but itโ€™s more prettier and easier to work by choosing a ModelSerializer to work with as they are tightly bound. Notice the difference between how simple it gets to.

class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('email', 'username', 'first_name', 'last_name')

We can customize the Meta class to have read-only fields with read_only attribute which accepts a tuple of field names (or '__all__' for making everything read only). drf handles everything for you, the correct matching of model fields, verifying read/write access, permissions and authentication. Simple isnโ€™t it?

Okay, letโ€™s not make this post any longer. Iโ€™ll add some more stuff this week. Hope to see you guys again. ๐Ÿ‘‹

Love always, Naveen. ๐Ÿ˜