How to Create Rest API with Django Tastypie?

Published On: 11/10/2022 | Category: Django


Hi Dev,

Here, I will show you how to create rest api with django tastypie. if you have question about tastypie django tutorial then I will give simple example with solution. This article goes in detailed on django tastypie vs rest framework. We will use getting started with django tastypie example.

This article is for you if you've gone through the fantastic Writing your first app with Django tutorial or if you already have a Django project and want to add a REST API. The abundance of packages available for Django is one advantage (look here). We'll be utilising django-tastypie, one of those packages.

Here i explained simply step by step example of how to create rest api with django tastypie.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example

Step 2: Create a App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python manage.py startapp core

Step 3: Install django-tastypie

Install Django and its prerequisites:

pip install django-tastypie
pip install defusedxml
pip install lxml

Step 4: Update setting.py

In this step we require to do two things in our settings.py file, One is our installed app name Add the below lines to your settings.py file:

Next, you need to add it in the settings.py file as follows:

example/settings.py
....
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
]

Step 5: Create a Model

In this step we will require the database model for storing contacts.Open the core/models.py file and add the following code:

core/models.py
from django.db import models

# Create your models here.

class Post(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

Ok, all set. We can engender a migrations file for this change, then integrate it to our database via migrate.

python manage.py makemigrations
python manage.py migrate

Here, look my django admin interface add some data in to the post model..



Step 6: Tastypie Setup

In this step, we need to create example/api.py where located same folder as a setting.py file.

example/api.py
from tastypie.resources import ModelResource
from tastypie.constants import ALL
from core.models import Post


class PostResource(ModelResource):
    class Meta:
        queryset = Post.objects.all()
        resource_name = 'post'
        filtering = {'title': ALL}

Step 7: Update URLs

In this section, we need a update urls.py file within the example directory example/urls.py with your text editor and paste below code.

core/urls.py
from django.contrib import admin
from django.urls import path,include,re_path

from django1.api import PostResource

post_resource = PostResource()

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^api/', include(post_resource.urls)),
]

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

To obtain the data in JSON format, go to http://localhost:8000/api/post/?format=json.

To obtain the data in XML format, go to http://localhost:8000/api/post/?format=xml.

Remember the filter we put on the WhateverResource class?

filtering = {'title': ALL}

We may sort the things by title, I suppose. Try different keywords:

http://localhost:8000/api/post/?format=json&title__contains=django
http://localhost:8000/api/post/?format=json&title__contains=test

I Hope It will help you....