![]() ![]() Mentor = models.CharField(blank=True, null=True, max_length=85) Student = models.ForeignKey(Student, blank=True, null=True, on_delete=models.CASCADE, related_name='student_details')Ĭlassroom = models.CharField(blank=True, null=True, max_length=85) Stack = models.CharField(choices=TYPES, max_length=55) Joined_on = models.DateTimeField(auto_now_add=True) Location = models.CharField(max_length=55) Gender = gender = models.CharField(choices=GENDER, max_length=55) Uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)Īccount = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')Īvatar = CloudinaryField('image', blank=True, null=True) The structure should look something like:įrom import Userįrom cloudinary.models import CloudinaryField What that means is that in the classroom class with the student as a foreign key, we have a deeply nested object. In the student class we’ve inherited the default Django User models class. In my case I have two classes: Student and Classroom. The next step is to create the models that we want to be serialized. The setup should look like the image below. ![]() We also need to create a serializers.py file which will serialize our classes. We’ll add the rest framework under the INSTALLED APPS part of the settings.py file in the project folder, and it should resemble the snapshot below when finished:Īs demonstrated below, we provide paths to links that return the appropriate view function when we submit requests to the API and app in the project’s urls.py file. With our virtual environment activated, the next step is to install the REST framework with the command below: pip install djangorestframeworkĪfter installation, the next step is to set up our application to work with the Django REST framework. The below image represents our project folder structure of the created project and application. Setting up the Virtual Environment Python3 -m venv virtualĪctivating the virtual environment is as easy as typing the below commands in the terminal. If you are reading this, I will assume that you are familiar with the basics of Django if not, see the original Django documentation for more details. We’ll start by developing a straightforward Django project. Let’s dive in and serialize data using DRF right away. Data can be serialized in DRF in just a few different methods, namely using Serializers class and models Serializers. The Django Rest Framework (DRF) is a strong and adaptable set of tools for creating APIs. This is very convenient for any developer who would be considering creating an API. Django REST offers serialization and deserialization of data and above that it offers data validation making sure that the type of data that is stored correctly corresponds to what is defined in the models. It describes an architecture which is used for the purpose of creating APIs for data communication. REST stands for REpresentational State Transfer. Deserialization just does the opposite.ĭjango is one of the upcoming web frameworks which comes along with its REST framework. Serialized data is commonly converted to its JSON (a format that encodes objects in a string) pendant. What is Data Serialization?ĭata Serialization is the process of converting data from one format to another readable with frontend frameworks. This skill has a steep, short learning curve, so get ready to dive right in. ![]() NOTE: This article is for mid to senior level developers who have a good understanding of using the Django framework and are interested in familiarizing themselves with the Django REST framework.īy the end of this article, you should be able to use Django REST Framework to serialize your data or any nested fields in Django models and have an API endpoint for your data. This saves money and time because development, testing, and maintenance time are reduced. As a result, we have APIs that promote the centralization of business logic in one place. Companies are constantly working on ways to develop dynamic applications that provide similar functionalities in both web and mobile platforms. You must then configure your django project to use the libary with following option in settings.In this day and age, when APIs are so popular, developers must be cautious about how they serialize and deserialize data, as well as how secure their data is. Pip install djangorestframework-simplejwt ![]() First install simple jwt with the command ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |