virtualenvwrapper configuration

There are files for hooks and configuration is very straight forward.
I’d like to create a new folder named src for storing all the code base. Then, change directory to the new folder. Let’s see what I can do.

There is postrmvirtualenv file under $WORKON_HOME. In my case, it is ~/venv.
Let’s open up the file in vi editor and add some hooks.

$ vi ~/venv/postrmvirtualenv

# Add following to the file and save.
mkdir $path
cd $path

I’ve created a variable for the path of the source folder I mentioned. Then, create it and change directory to it.

Now, mkvirtualenv command lets you to create a new virtual env box and create a new folder called src under the project just created and change directory to the folder.

Let’s make some changes for workon command same as above.

I’d like to change directory to the src folder under the project once the project has been activated. Also, I’d like to change directory to the virtualenv root folder ~/venv once the project has been deactivated.

Open up postactivate and add below for activate behavior.


Open up postactivate and add below for activate behavior.


That’s it and enjoy.


Django @property executes extra database calls? Ensure using cached result instead.

People made mistakes as below code that executes database call every time it’s invoked.

class MyModel(models.Model):
    def is_ready(self):
        status = self.check_status_from_db()
        return True if status else None

Assuming that there’re 2 or more {{ object.is_ready }} in your template especially in a loop on a list page. See what would happen via django debug toolbar.

Let’s fix the issue with the below code.

class MyModel(models.Model):
    def is_ready(self):
        if not hasattr(self, '_is_ready'):
            status = self.check_status_from_db()
            self._is_ready = True if status else None
        return self._is_ready

Now, retest the code and see what’s difference.

Django ETag doesn’t work on my local machine. Why?

Django Etag doesn’t work on my local machine via the virtual webserver.

The reason is that browser sends a request via HTTP/1.1 and django sends a response via HTTP/1.0.

How I can update the version on my machine? Here’s a monkey patch that tweak the version number.

Do not use it on your web server if you use a web webserver such as apache or nginx.


# HACK: without HTTP/1.1, Chrome ignores certain cache headers during development!
#       see for a bit more discussion.
from wsgiref import simple_server
simple_server.ServerHandler.http_version = "1.1"

I found this monkey patch from but unfortunately I lost the link. 😦

Inheritance Model, Polymorphism in Django

First of all, you need to install django-model-utils which allows you to create inheritance models.

ref. or

sudo pip install django-model-utils

Next, create your model.

from django.db import models
from model_utils.models import InheritanceCastModel

class Content(InheritanceCastModel):
    title = models.CharField(max_length=50)

class Video(Content):
    autoPlay = models.BooleanField(default=True)

class Audio(Content):
    Lyric = models.CharField(max_length=2000, null=True)


contents = Content.objects.all()
for content in contents:
    if hasattr(content, 'audio'):
        print 'This is audio'
        print 'This is video'

Also, you can add a filter as below.

list = Content.objects.filter(real_type__name = 'audio')

Rename Model(Table) in Django South Migration

When you change the name of model and create a migration file, you will see the code like “delete table, create table, and create relationship”.
It means you will lose all data in the previous model. However, here’s a simple way to rename model(table) with data.
Change the migration file as below or you may create yours.

class Migration(SchemaMigration):

    def forwards(self, orm):
        # Renaming model 'Tag' to 'RemarkTag'

    def backwards(self, orm):
        # Renaming model 'Tag' to 'RemarkTag'