Created
September 12, 2015 01:08
-
-
Save AzMoo/249a83cd1b2b5be81fff to your computer and use it in GitHub Desktop.
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 models, migrations | |
def replicate_company(apps, schema_editor): | |
""" | |
THIS FUNCTION IS BROKEN | |
AttributeError: type object 'NewCompany' has no attribute 'add_root' | |
""" | |
NewCompany = apps.get_model("shared", "NewCompany") | |
Company = apps.get_model("shared", "Company") | |
for c in Company.objects.all(): | |
NewCompany.add_root(id=c.id, name=c.name) | |
def _replicate_company(apps, schema_editor): | |
""" | |
THIS FUNCTION WORKS | |
""" | |
from shared.models import NewCompany | |
Company = apps.get_model("shared", "Company") | |
for c in Company.objects.all(): | |
NewCompany.add_root(id=c.id, name=c.name) | |
class Migration(migrations.Migration): | |
dependencies = [ | |
('shared', '0001_initial'), | |
] | |
operations = [ | |
migrations.CreateModel( | |
name='NewCompany', | |
fields=[ | |
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | |
('path', models.CharField(unique=True, max_length=255)), | |
('depth', models.PositiveIntegerField()), | |
('numchild', models.PositiveIntegerField(default=0)), | |
('name', models.CharField(unique=True, max_length=255)), | |
], | |
options={ | |
'abstract': False, | |
}, | |
), | |
migrations.RunPython(_replicate_company, lambda x,y: (x,y)), | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Django documentation warns against importing the model directly in a migration - should this behavior be brought up with the django-treebeard folks? In the mean time, going to see if I can get this to work with my projects.