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. ๐