Skip to content

Instantly share code, notes, and snippets.

smcoll /
Last active January 24, 2024 15:55
Django migration converting integer pk table to UUID pk table
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import uuid
from django.db import migrations, models
def fill_mymodel_uuid(apps, schema_editor):
db_alias = schema_editor.connection.alias
MyModel = apps.get_model('myapp', 'MyModel')
smcoll /
Created July 19, 2017 15:07
Django test runner which installs HSTORE extension
class TestRunner(runner.DiscoverRunner):
""" When migrations are disabled for the test runner, the `pre_migrate` signal
does not emit. So we need another hook for installing the extension. Prior to
Django 1.9, the `pre_syncdb` signal worked for that.
def setup_databases(self, **kwargs):
Always create PostgreSQL HSTORE extension if it doesn't already exist
on the database before syncing the database. Requires PostgreSQL >= 9.1
smcoll /
Created August 7, 2018 15:30
Django migration to merge one table's records into another similar table (both having UUID primary keys)
from django.db import migrations
def merge_foo_into_bar(apps, schema_editor):
""" For each FooCategory, create a corresponding BarCategory (with the new type)
and for each Foo, create a corresponding Bar.
The idea is that Foo and FooCategory would be dropped in a subsequent migration.
Foo = apps.get_model('myapp', 'Foo')
smcoll /
Created July 25, 2018 21:53
Django GIN index from pg_tgrm extension.
from django.contrib.postgres.indexes import GinIndex
class GinTrgmIndex(GinIndex):
""" GIN index from the pg_trgm extension.
NOTE: before adding this index, be sure to have the `pg_trgm` extension installed. This can be
done by including (before any GinTrgmIndex indexs in model meta) the migration operation
smcoll /
Created July 25, 2018 21:51
Django Postgres database wrapper with `pg_trgm` support.
from django.db.backends.postgresql_psycopg2 import base
class DatabaseWrapper(base.DatabaseWrapper):
""" Postgres database backend with support for operations using trigram indexes, inspired by
Put this file in a new module, and point to that module in `settings.DATABASES[<name>]['ENGINE']
smcoll /
Created February 10, 2017 20:39
Django test to detect uncreated/unmerged migration files
from StringIO import StringIO
from import call_command
from django.test import TestCase, override_settings
class TestMigrationDiscrepancies(TestCase):
def test_for_missing_migrations(self):
smcoll /
Last active March 24, 2017 22:29
Django migration for converting model to django-polymorphic child model
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
from django.db.models import F
def seed_baseitem(apps, schema_editor):
""" For each Foo, create a BaseItem with the same primary key value and
the appropriate `polymorphic_ctype` value. This will be used by a new
smcoll /
Last active August 4, 2016 18:38
test for output of ` check --deploy`
import sys
from subprocess import check_call
from tempfile import TemporaryFile
from django.test import TestCase
class SecurityHealthCheck(TestCase):
""" Run the deploy check on each settings file for a live deployment. If there are any warnings, fail and report.
smcoll /
Created January 13, 2014 18:55
Django ModelField for timezone-naive datetimes
import datetime
from django.core import exceptions
from django.db.models import DateTimeField
from django.utils.dateparse import parse_date, parse_datetime
from django.utils.translation import ugettext_lazy as _
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^myproject\.myapp\.fields\.NaiveDateTimeField"])
smcoll / gist:1849103
Created February 17, 2012 00:18
more flexible JSONRenderer class
class JSONRenderer(BaseRenderer):
Renderer which serializes to JSON
media_type = 'application/json'
format = 'json'
ensure_ascii = True
def render(self, obj=None, media_type=None):