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
Uncategorized

Vim Madness

I use the top notch IDEs for development but that is not geeky enough. So, ignoring my previous failed attempts, I am going to give the text editor one more try. Apparently, this blog post would be more like self documentation, mostly notes and hints to self. If anyone else also gets benefited, that’d be an added bonus.

Quick Shortcuts

  • Vundle Bundle Installation: “:BundleInstall”
  • New Tab: “:tabnew
  • Navigate Tabs: “:tabn” for next, “:tabp” for prev. “gt” in normal mode
  • Switch Windows: “Ctrl + w, Ctrl+w”
  • Splitting Windows: “:split “, “vsplit” for vertical splits
  • NerdTree – Open in new tab: “t”

Let there be ~/.vimrc

OK, here’s my ~/.vimrc, the initial version is messy, not organized at all, everything I came across, I stuffed in here. I shall update and clean the vimrc over time.