Now we need to expose this logic to the outer space, which is done via url routers: we have to define mappings from http request addresses to the views (controllers). serializers import UniversitySerializer, StudentSerializerĬlass StudentViewSet(viewsets.ModelViewSet):Ĭlass UniversityViewSet(viewsets.ModelViewSet): Let’s configure two viewsets for our classes: Listing of project/apps/core/views.py from rest_framework import viewsetsįrom. It’s a full CRUD set with http as the interface protocol. You are able to create, view, edit and delete objects in your system (and database). If you take a look at the ModelViewSet code you’ll see that there’s a lot of functionality automatically added there. Make a quick viewsetĪ viewset is a set of views (controllers in traditional MVC terminology). As a reference, you can use DRF’s documentation or even some performance-oriented serializers. Of course, you can manually define your own serializers. So, let’s define some model-based serializers: Listing of project/apps/core/serializers.py from rest_framework import serializersĬlass UniversitySerializer(serializers.ModelSerializer):Ĭlass StudentSerializer(serializers.ModelSerializer): This is a serialization/deserialization task. To perform HTTP-based interaction we have to define rules to convert Django models (python objects) to the json strings and vice versa. Next, you should add ‘rest_framework’ to the INSTALLED_APPS at the project/conf/base.py so that it’s available for Django process. Obviously, you cannot plug in anything unless you install it, so let’s install Django REST framework (or DRF) with pip: $ pip install djangorestframework It will allow you to identify the best talents very easily! Django REST framework-based api If your company is hiring a Python developer, check out this Python test prepared by our professionals. You can add universities here and students here.Ĭongratulations! Without writing much code at all, go take a look at your brilliant and free functionality. Go to the admin url (ensure your development server is up): Next, create an admin user (comand-line dialogue omitted, as it’s quite obvious): python project/manage.py createsuperuser To enable it, add this to the project/apps/core/admin.py: Listing of project/apps/core/admin.py from ntrib import admin Enable the admin interface and populate the databaseĭjango’s admin interface offers a visually convenient way to discover and edit database data. Now you have a database structure defined and we are ready to put some data in it. You will see a lot of SQL queries logged to the console, and that’s absolutely fine. Now to apply them, do this: python project/manage.py migrate core As we don’t have these tables in the database yet, let’s create a rule to make them (in Django these rules are called migrations): python project/manage.py makemigrations core Return '%s %s' % (self.first_name, self.last_name)Įach Model here is related to a table in the database. University = models.ForeignKey(University) Last_name = models.CharField(max_length=50) Listing of project/apps/core/models.py from django.db import modelsįirst_name = models.CharField(max_length=50) To see a list of available fields you can go to the Django manual for model fields. Every university has a name and every student has a first name, last name and attends a single university (many-to-one relationship). So, let’s first define entity classes and their attributes. Here we want to model students that go to a university. Next, start your development server with the following: $ python project/manage.py runserverįinally, go to You will see a “Welcome, Django” message. Now your environment is fully functional. $ django-admin startproject project -template= Use the code below to create an environment for further development. The animation below shows the automatically generated html interface of the api created in this tutorial: You can find a repository here and demo here. You can use this as a starting point, and then it’s quite easy to extend this project with different pluggable Django applications. In this article, you will learn how to create a basic application with Django and Django REST framework. This way you’ll be able to store your domain’s entities in the database. If you are working on a single-page application and you want it to have some persistence, it’s a really good idea to have a REST api.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |