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 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

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

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


Step 5: Create a Model

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

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 makemigrations
python 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/ where located same folder as a file.

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 file within the example directory example/ with your text editor and paste below code.

from django.contrib import admin
from django.urls import path,include,re_path

from django1.api import PostResource

post_resource = PostResource()

urlpatterns = [
    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:


I Hope It will help you....