Created
December 4, 2014 08:21
-
-
Save nonZero/6c314e10baf60da6a348 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
class Migration(migrations.Migration): | |
dependencies = [ | |
] | |
operations = [ | |
migrations.CreateModel( | |
name='Customer', | |
fields=[ | |
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | |
('name', models.CharField(max_length=1000)), | |
], | |
options={ | |
'abstract': False, | |
}, | |
bases=(models.Model,), | |
), | |
migrations.CreateModel( | |
name='Partner', | |
fields=[ | |
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | |
('name', models.CharField(max_length=1000)), | |
('parent', models.ForeignKey(blank=True, to='organizations.Partner', null=True)), | |
], | |
options={ | |
'abstract': False, | |
}, | |
bases=(models.Model,), | |
), | |
migrations.AddField( | |
model_name='customer', | |
name='parent', | |
field=models.ForeignKey(blank=True, to='organizations.Partner', null=True), | |
preserve_default=True, | |
), | |
] |
This file contains hidden or 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 | |
from django.conf import settings | |
def create_initial_organizations(apps, schema_editor): | |
if settings.DEBUG: | |
Customer = apps.get_model("organizations", "Customer") | |
Partner = apps.get_model("organizations", "Partner") | |
p = Partner(name=u"Partner") | |
p.save() | |
c = Customer.objects.create(name=u"Customer", parent=p) | |
c.save() | |
assert Customer.objects.exclude(parent=None).count() == 1 | |
class Migration(migrations.Migration): | |
dependencies = [ | |
('organizations', '0001_initial'), | |
] | |
operations = [ | |
migrations.RunPython(create_initial_organizations, | |
lambda _x, _y: None), | |
] | |
This file contains hidden or 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 models | |
class Organization(models.Model): | |
name = models.CharField(max_length=1000) | |
parent = models.ForeignKey("Partner", blank=True, null=True) | |
class Meta: | |
abstract = True | |
class Partner(Organization): | |
pass | |
class Customer(Organization): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment