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
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.
authentication_classes = (SessionAuthentication,
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.
DestroyModelMixin are the basic classes which are to be used to provide all of CRUD. They hold
destroy methods respectively. Their names speak for themselves, right.
Now speaking of permissions and authentication,
permission_classes properties could be set for the ViewSet according to the needs. Here comes the heart of every ViewSet, the
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
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.
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. 😍