Django REST API, or Django Restful API, is a powerful and flexible toolkit used for building customizable Web APIs, and it’s also efficient for creating mobile applications. Primarily, it helps developers to create a backend or server-side part of a mobile application. The mobile app communicates with the server-side application via this API for data or services.
The use of Django Restful API to build a mobile app involves creating a Django project, setting up the Django rest framework, creating a model, serializers, and views, and finally, setting up the URLs.
First, create a new Django project. You can use the following command in your terminal:
```
django-admin.py startproject myproject
cd myproject
```
Next, you need to integrate Django Rest Framework (DRF): It’s uncomplicated and can be readily set-up by installing it using pip(via the command – `pip install djangorestframework`) and adding `rest_framwork` to the `INSTALLED_APPS` setting of your Django project.
Then, create a new Django app using (django-admin.py startapp myapp) and define your models in models.py file. A model is a representation of a database table in Django. For instance, consider you want to create a blog where users can post articles. An Article can be a model with fields like title, content, created_at, updated_at, etc.
Subsequently, you need to create serializers. Serialization is the process of transforming data into a format that can be stored or transmitted and then reconstructing it. In Django REST framework, serializers work similar to Django Form classes, and fields in serializers are almost equivalent to Django Model fields.
Here’s an example code of serializers.py:
```
from rest_framework import serializers
from .models import Article
class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = Article
fields = (‘title’, ‘content’, ‘created_at’)
```
In Django REST framework, views handle the requests performed by the app. Following the previous example, the view function might look like this:
```
from .models import Article
from .serializers import ArticleSerializer
from rest_framework import viewsets
class ArticleViewSet(viewsets.ModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
```
Lastly, assign a URL to our views. Add the following code to your urls.py file:
```
from django.urls import path, include
from . import views
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(‘articles’, views.ArticleViewSet, basename=‘articles’)
urlpatterns = [
path(‘’, include(router.urls)),
]
```
Now, you have a working API that you can use for your mobile application development. Remember to run migrations every time you make changes to the database.
The information referred from the official Django documentation [https://docs.djangoproject.com/] and Django Rest Framework site [https://www.django-rest-framework.org/]. Both are considered a reliable source for Django and Django REST framework usage.