Snippet List
A very simple way of automatically loading data on model creation.
As I am using South I wanted an automatic way of loading initial data only when a new model is create during a migration.
Django provides almost everything out of the box by providing the *post_syncdb* signal (triggered also by South migrate command) and the *loaddata* command.
The code will simply look for a an existing fixture named `<model>_initial.*` and invoke the *loaddata* command to try to load it
Note: beware *post_syncdb* signal is deprecated since version 1.7: it has been replaced by *post_migrate*.
South documentation [contains a description](http://south.readthedocs.org/en/0.7.6/fixtures.html#fixtures-from-migrations) of the way you can load fixtures inside the data-migrations.
def forwards(self, orm):
from django.core.management import call_command
call_command("loaddata", "my_fixture.json")
It seems pretty clear and easy, but in fact it does not work the way you expect from south migrations, because the fixture loading does not engage the **orm** object. So, it allows **loaddata** management command to use standard models loading mechanism, and it would provide the most recent version of the models, obviously, which may not correspond to the schema of the fixture`s data.
To be ensured that migration will use appropriate version of the models for fixture loading you could use code like follows:
class Migration(DataMigration):
def forwards(self, orm):
load_fixture('my_fixture.json', orm)
class Migration(DataMigration):
def forwards(self, orm):
with southern_models(orm):
call_command("loaddata", "my_fixture.json")
- fixtures
- migration
- fixture
- south
- datamigration
This snippet *updates* http://www.djangosnippets.org/snippets/383/ and http://www.djangosnippets.org/snippets/1495/ for Django 1.4+, and adds support for sqlite3 and south. Original snippet text: A CompressedTextField to transparently save data gzipped in the database and uncompress at retrieval.
- text
- model
- field
- compressed
- gzip
- south
TimeZoneField is not supported with South 0.7. This snippet adds a custom rule to the introspection rules for TimeZoneField. The code is found in the following thread, and fixed according to the latest version of TimeZoneField.
http://groups.google.com/group/south-users/browse_thread/thread/34a05331140ee4dd
**Usage**
Simplest way is to save this snippet in a south_rules.py file and add an import in the models.py.
- timezones
- south
- timezonefield
I found that South doesn't execute initial sql file after a migration, like what Django can do after syncdb. So here's the workaround by using post_migrate signal.
Usage:
Put your SQL files the same way as you would if you were to use Django to load your initial SQL file (follow Django doc). The only difference is that, the SQL filename needs to in this format: <app_label>.sql OR <app_label>.<backend>.sql
This is done this way since the migration is run per app.
- custom-sql
- south
- initial-data
- post_migrate
Allows you to dynamically maintain a local_constants.py file from a migration tool like South. Example of usage:
set_constant('/home/projects/sample/local_constants.py', 'STAMP_MW_ID', 42, 'Set from sample.0007_add_constants.py')
More more information, see [Allows you to dynamically maintain a local_constants.py file from a migration tool like South. Example of usage:
set_constant('/home/projects/sample/local_constants.py', 'STAMP_MW_ID', 42, 'Set from sample.0007_add_constants.py')
More more information, see [http://menendez.com/blog/maintain-contants-through-south-data-migration/](http://menendez.com/blog/maintain-contants-through-south-data-migration/).
This is useful to run before you add a unique key to a character field that has duplicates in it. It just adds numbers to the end of the contents, so they will be unique.
It takes a model class and a field name. The model class can be a South fake orm object, so this can be used inside data migrations.
- rename
- duplicate
- south
- unique-key
8 snippets posted so far.