Categories
Python

Django REST Framework: Quick Introduction

Okay, so you know how cool Django is? Well, Django Rest Framework brings the Django like simplicity in building REST APIs.

Installation

Use pip to install:

Add to Installed Apps:

ModelViewSet: Doing The Magic

ModelViewSet takes a Model and automates the required REST access to it. Let’s see how simple it is.

We have a Model like this:

We will create a class based view for allowing REST access to this Model:

Add to urls.py to add URL handlers for the view:

Now run syncdb and runserver commands from manage.py. If you visit: localhost:8000 (or whatever the address of the development server on your machine), you should be able to see the api root. In the URLConf defined above, we should be able to access the Photo model on “/photo”. Use a REST client to try it out.

Authentication

Django Rest Framework supports multiple authentication systems. Here, we shall see how we can enable basic http auth to the REST APIs.

Add this to settings.py:

That’s it. Now we have the Django Auth system hooked into Django Rest Framework. Any user registered with the default django auth is a now valid user for the REST APIs as well. But how do we find the authenticated user? It’s simple as always. You can grab “request.user” for the user data and “request.auth” for extra auth data (specially when working with oauth implementation).

Let’s see how we can use in the class based views we demonstrated above:

Here we have overridden the list() method to demonstrate how to use the request object. If any user tries to access without authenticating, the request.user will be set to AnonymousUser. If authenticated, it will be set to the authenticated user.

By overriding the methods of a ModelViewSet, we can customize the response based on authentication status. We can also use Permission & Throttling policies for easier control. I’ll try to write about those in future blog posts.

Categories
Python

Django: Flush (Clear) Database

In Django, we often need to remove all the tables (with data) and recreate them from scratch. Specially, after changing a model structure, if we’re not using any database migration tool, we usually manually drop the table and run syncdb again. But there’s a quick way –

Django 1.5 and Higher

We have to collect the output of sqlclear and then pass it to dbshell.

Django 1.4

On earlier versions of Django, you could use “reset ” for removing tables from a particular app.

DEPRECATED: The command is deprecated and newer versions use the flush command. Check out the Django manual for more details.

Categories
Bangla Python

āĻĒāĻžāχāĻĨāύ⧇ āϏāĻŋāĻ™ā§āĻ—āϞāϟāύ āĻĒā§āϝāĻžāϟāĻžāĻ°ā§āύ

āϏāĻŋāĻ™ā§āĻ—āϞāϟāύ āĻĒā§āϝāĻžāϟāĻžāĻ°ā§āύ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āφāĻŽāϰāĻž āύāĻŋāĻļā§āϚāĻŋāϤ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŋ āϝ⧇āύ āĻāĻ•āϟāĻŋ āĻ•ā§āϞāĻžāϏ āĻĨ⧇āϕ⧇ āĻāĻ•āϟāĻŋāχ āĻŽāĻžāĻ¤ā§āϰ āĻ…āĻŦāĻœā§‡āĻ•ā§āϟ āϤ⧈āϰāĻŋ āĻ•āϰāĻž āĻšāϝāĻŧ āĨ¤ āϏāϚāϰāĻžāϚāϰ āφāĻŽāϰāĻž āĻāĻ•āχ āĻ•ā§āϞāĻžāϏ āĻĨ⧇āϕ⧇ āĻĒā§āϰāϝāĻŧā§‹āϜāύāĻŽāϤ āχāύāĻ¸ā§āĻŸā§āϝāĻžāĻ¨ā§āϏ āϤ⧈āϰāĻŋ āĻ•āϰ⧇ āύ⧇āχ āĨ¤ āĻ•āĻŋāĻ¨ā§āϤ⧁ āĻ•āĻ–āύ⧋ āĻ•āĻ–āύ⧋ āĻĒā§āϰāϝāĻŧā§‹āϜāύ āĻĒāĻĄāĻŧ⧇ āϏāĻŋāĻ¸ā§āĻŸā§‡āĻŽāĻ“āϝāĻŧāĻžāχāĻĄ āĻāĻ•āχ āχāύāĻ¸ā§āĻŸā§āϝāĻžāĻ¨ā§āϏ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻžāϰ āĻ•āĻŋāĻ‚āĻŦāĻž āĻ•āĻ–āύ⧋ āĻ•āĻ–āύ⧋ āύāϤ⧁āύ āχāύāĻ¸ā§āĻŸā§āϝāĻžāĻ¨ā§āϏ āϤ⧈āϰāĻŋ āĻ•āϰāĻž āĻŦ⧇āĻļ āϰāĻŋāϏ⧋āĻ°ā§āϏ āχāĻ¨ā§āĻŸā§‡āĻ¨ā§āϏāĻŋāĻ­ āĻšāϝāĻŧ āĨ¤ āĻāϰāĻ•āĻŽ āĻĒāϰāĻŋāĻ¸ā§āĻĨāĻŋāϤāĻŋāϤ⧇ āφāĻŽāϰāĻž āϏāĻŋāĻ™ā§āĻ—āϞāϟāύ āĻĒā§āϝāĻžāϟāĻžāĻ°ā§āύ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŋ āĨ¤ āĻāϰ āĻŦāĻžāĻ¸ā§āϤāĻŦ āωāĻĻāĻžāĻšāϰāĻŖ āĻšāϤ⧇ āĻĒāĻžāϰ⧇ āĻĄāĻžāϟāĻžāĻŦ⧇āχāϜ āĻ•āĻžāύ⧇āĻ•āĻļāύ āĨ¤ āφāĻŽāϰāĻž āϚāĻžāχāĻŦā§‹ āφāĻŽāĻžāĻĻ⧇āϰ āĻā§āϝāĻžāĻĒā§āϞāĻŋāϕ⧇āĻļāύ āϝ⧇āύ āĻāĻ•āĻŦāĻžāϰāχ āĻĄāĻžāϟāĻžāĻŦ⧇āχāϜ āĻ āĻ•āĻžāύ⧇āĻ•ā§āϟ āĻ•āϰ⧇ āĻāĻŦāĻ‚ āĻĒāϰ⧇ āϐ āĻ•āĻžāύ⧇āĻ•āĻļāύāϟāĻŋāχ āĻŦāĻžāϰāĻŦāĻžāϰ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āĨ¤

āĻĒāĻžāχāĻĨāύ⧇ āϏāĻŋāĻ™ā§āĻ—āϞāϟāύ āĻĒā§āϝāĻžāϟāĻžāĻ°ā§āύāϟāĻž āĻŦ⧇āĻļ āĻ•āϝāĻŧ⧇āĻ•āĻ­āĻžāĻŦ⧇ āĻ•āϰāĻž āϝāĻžāϝāĻŧ āĨ¤ āϤāĻŦ⧇ āϏāĻŦāϚāĻžāχāϤ⧇ āĻ—ā§āϰāĻšāύāϝ⧋āĻ—ā§āϝ āωāĻĒāĻžāϝāĻŧ āĻšāĻšā§āϛ⧇ āĻĄā§‡āϕ⧋āϰ⧇āϟāϰ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻž āĨ¤ āφāĻŽāϰāĻž āĻĒā§āϰāĻĨāĻŽā§‡āχ āϕ⧋āĻĄ āĻĻ⧇āϖ⧇ āύ⧇āχ –

āĻĒāĻžāχāĻĨāύ⧇ āĻĄā§‡āϕ⧋āϰ⧇āϟāϰ āφāϏāϞ⧇ callable āĻ…āĻŦāĻœā§‡āĻ•ā§āϟ āϝ⧇āϟāĻŋ āĻ…āĻ¨ā§āϝ āĻāĻ•āϟāĻŋ āĻ…āĻŦāĻœā§‡āĻ•ā§āϟāϕ⧇ āφāĻ°ā§āϗ⧁āĻŽā§‡āĻ¨ā§āϟ āĻšāĻŋāϏ⧇āĻŦ⧇ āĻ—ā§āϰāĻšāύ āĻ•āϰ⧇ āĻāĻŦāĻ‚ āĻāĻŦāĻ‚ āφāϰ⧇āĻ•āϟāĻŋ āĻ…āĻŦāĻœā§‡āĻ•ā§āϟ āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ•āϰ⧇ āĨ¤ āϕ⧋āύ āĻ•ā§āϞāĻžāϏ āĻŦāĻž āĻĢāĻžāĻ‚āĻļāύ āĻāϰ āωāĻĒāϰ @ āϚāĻŋāĻšā§āύ āϏāĻš āĻĄā§‡āϕ⧋āϰ⧇āϟāϰ⧇āϰ āύāĻžāĻŽ āϞāĻŋāϖ⧇ āφāĻŽāϰāĻž āĻĄā§‡āϕ⧋āϰ⧇āϟāϰ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻŋ āĨ¤ āĻĄā§‡āϕ⧋āϰ⧇āϟāϰ āϕ⧇ āφāĻŽāϰāĻž āϏāĻšāĻœā§‡ āĻāĻ­āĻžāĻŦ⧇ āĻ•āĻ˛ā§āĻĒāύāĻž āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŋ –

āĻāϰ āĻŽāĻžāύ⧇ āĻĻāĻžāĻĄāĻŧāĻžāĻŦ⧇ āĻ…āύ⧇āĻ•āϟāĻž āĻāϰāĻ•āĻŽ –

āϤ⧋ āĻāχ āϜāĻŋāύāĻŋāϏāϟāĻŋāχ āφāĻŽāϰāĻž āϏāĻŋāĻ™ā§āĻ—āϞāϟāύ āĻĒā§āϝāĻžāϟāĻžāĻ°ā§āύ āϤ⧈āϰāĻŋ āĻ•āϰāϤ⧇ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻ›āĻŋ āĨ¤ āĻĒā§āϰāĻĨāĻŽā§‡ āφāĻŽāϰāĻž āĻāĻ•āϟāĻŋ āĻ•ā§āϞāĻžāϏ āĻŦ⧇āχāϜāĻĄ āĻĄā§‡āϕ⧋āϰ⧇āϟāϰ āĻĄāĻŋāĻĢāĻžāχāύ āĻ•āϰāϞāĻžāĻŽ āĻāĻŦāĻ‚ āϤāĻžāϰāĻĒāϰ āφāĻŽāĻžāĻĻ⧇āϰ Person āĻ•ā§āϞāĻžāϏ⧇ āĻĄā§‡āϕ⧋āϰ⧇āϟāϰ āĻā§āϝāĻžāĻĒā§āϞāĻžāχ āĻ•āϰāϞāĻžāĻŽ āĨ¤ āφāϏāϞ⧇ āĻ•āĻŋ āϘāϟāϛ⧇ āĻāĻ–āĻžāύ⧇? āĻŦā§āϝāĻžāĻĒāĻžāϰāϟāĻž āφāϏāϞ⧇ āĻ…āύ⧇āĻ•āϟāĻž āĻāϰāĻ•āĻŽ –

āĻšāϝāĻŧ⧇ āϝāĻžāĻšā§āϛ⧇ –

āφāĻŽāϰāĻž āφāϰ āϚāĻžāχāϞ⧇āĻ“ Person āχāύāĻ¸ā§āĻŸā§āϝāĻžāĻ¨ā§āϏ āϤ⧈āϰāĻŋ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦā§‹ āύāĻž āĨ¤ āϤāĻžāχ āĻŦāĻžāĻ§ā§āϝ āĻšāϝāĻŧ⧇ āφāĻŽāϰāĻž Instance() āĻ•āϞ āĻ•āϰāĻŦā§‹ āĨ¤ āφāϏāϞ⧇ āφāĻŽāĻžāĻĻ⧇āϰ āĻ•āϞāϟāĻŋ āĻšāĻŦ⧇ āĻ…āύ⧇āĻ•āϟāĻž āĻāϰāĻ•āĻŽ –

singleton āĻ•ā§āϞāĻžāϏ āϟāĻŋ __init__ āĻ•āϰāĻžāϰ āϏāĻŽāϝāĻŧāχ āφāĻĻāĻŋ āĻāĻŦāĻ‚ āĻ…āĻ•ā§ƒāĻ¤ā§āϰāĻŋāĻŽ Person āĻ•ā§āϞāĻžāϏāϟāĻŋ āĻĒāĻžāϏ āĻ•āϰ⧇ āĻĻ⧇āĻ“āϝāĻŧāĻž āĻšāϝāĻŧ āϝ⧇āϟāĻŋ self._decorated āĻ āĻĨ⧇āϕ⧇ āϝāĻžāϝāĻŧ āĨ¤ āĻāϰāĻĒāϰ Instance() āĻŽā§‡āĻĨāĻĄā§‡āϰ āĻ•āĻžāϜ āĻŦ⧇āĻļ āϏāĻšāϜ āϏāϰāϞ āĨ¤ āϏ⧇ āĻĻ⧇āϖ⧇ āφāϗ⧇āχ āχāύāĻ¸ā§āĻŸā§āϝāĻžāĻ¨ā§āϏ āϤ⧈āϰāĻŋ āĻ•āϰāĻž āφāϛ⧇ āĻ•āĻŋāύāĻž āĨ¤ āύāĻž āĻĨāĻžāĻ•āϞ⧇ self._decorated āĻ āĻĨāĻžāĻ•āĻž āĻŽā§‚āϞ āĻ•ā§āϞāĻžāϏ āĻĨ⧇āϕ⧇ āĻāĻ•āϟāĻŋ āχāύāĻ¸ā§āĻŸā§āϝāĻžāĻ¨ā§āϏ āĻ•āϰ⧇ āϰāĻŋāϟāĻžāĻ°ā§āύ āĻ•āϰ⧇ āĻāĻŦāĻ‚ āύāĻŋāĻœā§‡āϰ āĻ•āĻžāϛ⧇ self._instance āĻ āϏ⧇āχāĻ­ āĻ•āϰ⧇ āĨ¤

āĻ•ā§āϞāĻžāϏ āĻŦ⧇āχāϜāĻĄ āĻĄā§‡āϕ⧋āϰ⧇āϟāϰ āĻŦ⧁āĻāϤ⧇ āϏāĻŽāĻ¸ā§āϝāĻž āĻšāϞ⧇ āĻāĻ­āĻžāĻŦ⧇ āĻāĻ•ā§āϏāĻĒ⧇āϰāĻŋāĻŽā§‡āĻ¨ā§āϟ āĻ•āϰ⧇ āĻĻ⧇āĻ–āĻž āϝāĻžāϝāĻŧ –