This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
`django.contrib.postgres.operations.TrigramExtension` | |
https://vxlabs.com/2018/01/31/creating-a-django-migration-for-a-gist-gin-index-with-a-special-index-operator/ | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.backends.postgresql_psycopg2 import base | |
class DatabaseWrapper(base.DatabaseWrapper): | |
""" Postgres database backend with support for operations using trigram indexes, inspired by | |
https://stackoverflow.com/a/44962928 | |
Put this file in a new module, and point to that module in `settings.DATABASES[<name>]['ENGINE'] | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from StringIO import StringIO | |
from django.core.management import call_command | |
from django.test import TestCase, override_settings | |
class TestMigrationDiscrepancies(TestCase): | |
@override_settings(MIGRATION_MODULES={}) | |
def test_for_missing_migrations(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DocumentingTemplateRenderer: | |
... | |
escape_binary = False | |
def _escape_binary(self, content): | |
if self.escape_binary and not all(char in string.printable for char in content): | |
return '[%d bytes of binary content]' % len(content) | |
return content | |
NewerOlder