From 41752923237b4993ff0220371ae199942909488d Mon Sep 17 00:00:00 2001 From: Brad Pitcher Date: Wed, 20 Aug 2014 10:35:59 -0700 Subject: [PATCH] make migrations custom user friendly --- lazysignup/migrations/0001_initial.py | 48 +++++++++++-------- .../0002_auto__add_field_lazyuser_created.py | 41 +++++++++------- 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/lazysignup/migrations/0001_initial.py b/lazysignup/migrations/0001_initial.py index 6c6aa73..50383f0 100644 --- a/lazysignup/migrations/0001_initial.py +++ b/lazysignup/migrations/0001_initial.py @@ -5,20 +5,35 @@ from south.v2 import SchemaMigration from django.db import models + +# Safe User import for Django < 1.5 +try: + from django.contrib.auth import get_user_model +except ImportError: + from django.contrib.auth.models import User +else: + User = get_user_model() + +# With the default User model these will be 'auth.User' and 'auth.user' +# so instead of using orm['auth.User'] we can use orm[user_orm_label] +user_orm_label = '%s.%s' % (User._meta.app_label, User._meta.object_name) +user_model_label = '%s.%s' % (User._meta.app_label, User._meta.module_name) + + class Migration(SchemaMigration): def forwards(self, orm): - + # Adding model 'LazyUser' db.create_table('lazysignup_lazyuser', ( ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), - ('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'], unique=True)), + ('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm[user_orm_label], unique=True)), )) db.send_create_signal('lazysignup', ['LazyUser']) def backwards(self, orm): - + # Deleting model 'LazyUser' db.delete_table('lazysignup_lazyuser') @@ -37,21 +52,16 @@ def backwards(self, orm): 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) }, - 'auth.user': { - 'Meta': {'object_name': 'User'}, - 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), - 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), - 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), - 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), - 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), - 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), - 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), - 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), - 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), - 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), - 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), - 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), - 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + user_model_label: { + 'Meta': { + 'object_name': User.__name__, + 'db_table': "'%s'" % User._meta.db_table + }, + User._meta.pk.attname: ( + 'django.db.models.fields.AutoField', [], + {'primary_key': 'True', + 'db_column': "'%s'" % User._meta.pk.column} + ), }, 'contenttypes.contenttype': { 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, @@ -63,7 +73,7 @@ def backwards(self, orm): 'lazysignup.lazyuser': { 'Meta': {'object_name': 'LazyUser'}, 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), - 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'unique': 'True'}) + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['%s']" % user_orm_label, 'unique': 'True'}) } } diff --git a/lazysignup/migrations/0002_auto__add_field_lazyuser_created.py b/lazysignup/migrations/0002_auto__add_field_lazyuser_created.py index 94752c2..c5eecf9 100644 --- a/lazysignup/migrations/0002_auto__add_field_lazyuser_created.py +++ b/lazysignup/migrations/0002_auto__add_field_lazyuser_created.py @@ -5,6 +5,20 @@ from django.db import models +# Safe User import for Django < 1.5 +try: + from django.contrib.auth import get_user_model +except ImportError: + from django.contrib.auth.models import User +else: + User = get_user_model() + +# With the default User model these will be 'auth.User' and 'auth.user' +# so instead of using orm['auth.User'] we can use orm[user_orm_label] +user_orm_label = '%s.%s' % (User._meta.app_label, User._meta.object_name) +user_model_label = '%s.%s' % (User._meta.app_label, User._meta.module_name) + + class Migration(SchemaMigration): def forwards(self, orm): @@ -33,21 +47,16 @@ def backwards(self, orm): 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) }, - 'auth.user': { - 'Meta': {'object_name': 'User'}, - 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), - 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), - 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), - 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), - 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), - 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), - 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), - 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), - 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), - 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), - 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), - 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), - 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + user_model_label: { + 'Meta': { + 'object_name': User.__name__, + 'db_table': "'%s'" % User._meta.db_table + }, + User._meta.pk.attname: ( + 'django.db.models.fields.AutoField', [], + {'primary_key': 'True', + 'db_column': "'%s'" % User._meta.pk.column} + ), }, 'contenttypes.contenttype': { 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, @@ -60,7 +69,7 @@ def backwards(self, orm): 'Meta': {'object_name': 'LazyUser'}, 'created': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), - 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'unique': 'True'}) + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['%s']" % user_orm_label, 'unique': 'True'}) } }