# In order to serialize objects, we must set 'safe=False'Ĭustomer_data = JSONParser().parse(request)Ĭustomer_serializer = CustomerSerializer(data=customer_data) Return JsonResponse(customers_serializer.data, safe=False) – customer_list_age(): find all customers by ‘age’įrom import JsonResponseįrom import csrf_exemptįrom rest_framework.parsers import JSONParserįrom rializers import customer_list(request):Ĭustomers_serializer = CustomerSerializer(customers, many=True) – customer_detail(): get/update/delete customer by ‘id’ – customer_list(): get list of customers, save a new customer, delete all customers Open customers/views.py file and declare two functions: – fields: specifies a tuple of field names that we want to include in the serialization Create API Views – model: specifies the model related to the serializer Now, under customers package, create serializers.py file: from rest_framework import serializersĬlass CustomerSerializer(serializers.ModelSerializer): – ModelSerializer class automatically populates a set of default fields and default validators, we only need to specify the model class. – This CustomerSerializer will inherit from rest_ superclass. We need a Serializer class for Customer instances to manage serialization to JSON and deserialization from JSON. Run the following Python script to apply the generated migration:Ĭheck PostgreSQL Database, now we can see that a table for Customer model was generated and it’s named customers_customer: Call to migrations.CreateModel() method will create a table that allows the underlying database to persist the model. It has an operation for creating Customer model table.
The generated code defines a subclass of the django.db.migrations.Migration. ('active', models.BooleanField(default=False)), ('name', models.CharField(default='', max_length=70)), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), It indicates that the customers/migrations/0001_initial.py file includes code to create Customer data model: We can see output text: Migrations for 'customers':
Python manage.py makemigrations customers Name = models.CharField(max_length=70, blank=False, default='')Īge = models.IntegerField(blank=False, default=1)Īctive = models.BooleanField(default=False) Implement Django RestApi App Data Model Create Data ModelĬustomers/models.py from django.db import models Inside gkzRestApi/settings.py, add: INSTALLED_APPS = [ Open gkzRestApi/settings.py, find INSTALLED_APPS, then add: Open customers/apps.py, we can see CustomersConfig class (subclass of the ) that represents our Django app and its configuration:Īdd Django Rest framework & RestApi App to Django project Run following commands to create new Django App named customers: 'ENGINE': 'django.db.backends.postgresql', Open gkzRestApi/settings.py and change declaration of DATABASES:
In this tutorial, we use psycopg2: pip install psycopg2. We have to install Python PostgreSQL adapter to work with PostgreSQL database. Pip install djangorestframework Create RestApi projectĬreate Django project named gkzRestApi with command: Setup Django RestApi project Install Django REST frameworkĭjango REST framework works on top of Django and helps us to build RESTful Web Services flexibly. – gkzRestApi/urls.py: defines root URL configurations that includes the URL patterns declared in customers/urls.py. – customers/urls.py: defines urlpatterns to be matched with request functions in the views.py. – customers/views.py: contains methods to process HTTP requests and produce HTTP responses (using CustomerSerializer). – customers/serializers.py: declares CustomerSerializer class (subclass of rest_) for Customer instances to manage serialization to JSON and deserialization from JSON.
– migrations/0001_initial.py: is generated by makemigrations command, includes the code to create the Customer model, will be run by migrate to generate PostgreSQL database table for Customer model. – customers/models.py: defines Customer data model class (subclass of the django.db.models.Model). – gkzRestApi/settings.py: configures settings for the Django project, including INSTALLED_APPS list with Django REST framework and Customers Application. – customers/apps.py: declares CustomersConfig class (subclass of the ) that represents our Django app and its configuration.