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'

Django says “Unable to open” SQLite3

First of all, make sure the db path in your settings file.

import os
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': os.path.join(PROJECT_ROOT, 'talktrack.sqlite'), # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.

Then, check permissions below file and folders. (django needs full permissions)
1. db file.
i.e. yourapp.sqlite
2. /var/tmp
3. parent folder of db file
i.e. /var/www/yourapp
if the db file is in the folder. /var/www/yourapp/yourapp.sqlite

sudo chown -R user:group file-or-folder

.NET Formatting Strings

formatting strings

Example Output
String.Format(“–{0,10}–“, “test”); —      test–
String.Format(“–{0,-10}–“, “test”); –test      —

formatting numbers

Specifier Type Format Output

(double 1.2345)


(int -12345)

c Currency {0:c} $1.23 -$12,345.00
d Decimal (whole number) {0:d} System.FormatException -12345
e Exponent/scientific {0:e} 1.234500e+000 -1.234500e+004
f Fixed point {0:f} 1.23 -12345.00
g General {0:g} 1.2345 -12345
n Number {0:n} 1.23 -12,345.00
r Round trippable {0:r} 1.23 System.FormatException
x Hexadecimal {0:x4} System.FormatException ffffcfc7

custom number formatting

Specifier Type Format Output

(double 1234.56)

0 Zero placeholder {0:00.000} 1234.560
# Digit placeholder {0:#.##} 1234.56
. Decimal point placeholder {0:0.0} 1234.6
, Thousand separator {0:0,0} 1,235
% Percentage {0:0%} 123456%

Group separator

String.Format(“{0:$#,##0.00;($#,##0.00);Nothing}”, value);

This will output “$1,240.00” if passed 1243.56.  It will output the same format bracketed if the value is negative “($1,240.00)”, and will output the string “Nothing” if the number is zero.

date formatting

Specifier Type Output

(June 8, 1970 12:30:59)

d Short date 08/06/1970
D Long date 08 June 1970
t Short time 12:30
T Long time 12:30:59
f Full date and time 08 June 1970 12:30
F Full date and time (long) 08 June 1970 12:30:59
g Default date and time 08/06/1970 12:30
G Default date and time (long) 08/06/1970 12:30:59
M Day/Month 8 June
r RFC1123 date string Mon, 08 June 1970 12:30:59 GMT
s Sortable date/time 1970-06-08T12:30:59
u Universal time, local timezone 1970-06-08 12:30:59Z
Y Month/Year June 1970

custom date formatting

Specifier Type Output

(June 8, 1970 12:30:59)

dd Day 08
ddd Short day name Mon
dddd Full day name Monday
hh 2 digit hour 12
HH 2 digit hour (24 hour) 12
mm 2 digit minute 30
MM Month 06
MMM Short month name Jun
MMMM Month name June
ss Seconds 59
yy 2 digit year 70
yyyy 4 digit year 1970
: Separator, e.g. {0:hh:mm:ss} 12:30:59
/ Separator, e.g. {0:dd/MM/yyyy} 08/06/1970