Django Tips


These are some of the tips that I need to remember. But there are a lot is not listed here so have fun in digging about them.

Persistent connections

Persistent connections avoid the overhead of re-establishing a connection to the database in each request.

Change CONN_MAX_AGE to enable Persistent connections.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db_name',
        'CONN_MAX_AGE': 600,  # 10 minutes
    }
}

Securely generate random string

from django.utils.crypto import get_random_string
# Then use it to get the random generated string
get_random_string()
# length and allowed_chars
get_random_string(length=4, allowed_chars='Q2W')

Event Sourcing

https://yoongkang.com/blog/event-sourcing-in-django/

django datetime aware naive

Testing:

Debug:

http://dailytechvideo.com/video-192-matt-boehm-python-debuggers/

STATICFILES_STORAGE ManifestStaticFilesStorage

Adding HTTP Auth to Django:

pip install install barrel

"""WSGI application file"""

import os
from django.core.wsgi import get_wsgi_application
from barrel import cooper

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

django_app = get_wsgi_application()

auth_decorator = cooper.basicauth(
    users=[('someuser', 'somepass'),],
    realm='Password Protected'
)

application = auth_decorator(django_app)

Writing Unit Tests for Django Migrations

More resources:

comments powered by Disqus