Skip to content

Commit f7142be

Browse files
committed
Merge pull request #128 from treyhunner/fix-additional-migration
More fixes for `CustomForeignKey`
2 parents 5f7d4a3 + d7d5830 commit f7142be

File tree

7 files changed

+93
-2
lines changed

7 files changed

+93
-2
lines changed

runtests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'simple_history',
2222
'simple_history.tests',
2323
'simple_history.tests.external',
24+
'simple_history.tests.migration_test_app',
2425
]
2526

2627
DEFAULT_SETTINGS = dict(

simple_history/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ def get_history_user(self, instance):
219219

220220
class CustomForeignKeyField(models.ForeignKey):
221221

222+
def __init__(self, *args, **kwargs):
223+
super(CustomForeignKeyField, self).__init__(*args, **kwargs)
224+
self.db_constraint = False
225+
self.generate_reverse_relation = False
226+
222227
def get_attname(self):
223228
return self.name
224229

simple_history/tests/migration_test_app/__init__.py

Whitespace-only changes.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
import simple_history.models
6+
from django.conf import settings
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='DoYouKnow',
18+
fields=[
19+
('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
20+
],
21+
options={
22+
},
23+
bases=(models.Model,),
24+
),
25+
migrations.CreateModel(
26+
name='HistoricalYar',
27+
fields=[
28+
('id', models.IntegerField(verbose_name='ID', auto_created=True, db_index=True, blank=True)),
29+
('history_id', models.AutoField(serialize=False, primary_key=True)),
30+
('history_date', models.DateTimeField()),
31+
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
32+
('history_user', models.ForeignKey(null=True, to=settings.AUTH_USER_MODEL)),
33+
],
34+
options={
35+
'verbose_name': 'historical yar',
36+
'ordering': ('-history_date', '-history_id'),
37+
},
38+
bases=(models.Model,),
39+
),
40+
migrations.CreateModel(
41+
name='WhatIMean',
42+
fields=[
43+
('doyouknow_ptr', models.OneToOneField(primary_key=True, to='migration_test_app.DoYouKnow', auto_created=True, parent_link=True, serialize=False)),
44+
],
45+
options={
46+
},
47+
bases=('migration_test_app.doyouknow',),
48+
),
49+
migrations.CreateModel(
50+
name='Yar',
51+
fields=[
52+
('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
53+
('what', models.ForeignKey(to='migration_test_app.WhatIMean')),
54+
],
55+
options={
56+
},
57+
bases=(models.Model,),
58+
),
59+
migrations.AddField(
60+
model_name='historicalyar',
61+
name='what_id',
62+
field=simple_history.models.CustomForeignKeyField(to='migration_test_app.WhatIMean', blank=True, null=True, related_name='+'),
63+
preserve_default=True,
64+
),
65+
]

simple_history/tests/migration_test_app/migrations/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from django.db import models
2+
from simple_history.models import HistoricalRecords
3+
4+
5+
class DoYouKnow(models.Model):
6+
pass
7+
8+
9+
class WhatIMean(DoYouKnow):
10+
pass
11+
12+
13+
class Yar(models.Model):
14+
what = models.ForeignKey(WhatIMean)
15+
history = HistoricalRecords()

simple_history/tests/tests/test_commands.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ def test_no_historical(self):
108108
out.getvalue())
109109

110110

111+
@skipUnless(django.get_version() >= "1.7", "Requires 1.7 migrations")
111112
class TestMigrate(TestCase):
112113

113-
@skipUnless(django.get_version() >= "1.7", "Requires 1.7 migrations")
114+
def test_makemigration_command(self):
115+
management.call_command(
116+
'makemigrations', 'migration_test_app', stdout=StringIO())
117+
114118
def test_migrate_command(self):
115-
management.call_command('migrate', fake=True, stdout=StringIO())
119+
management.call_command(
120+
'migrate', 'migration_test_app', fake=True, stdout=StringIO())

0 commit comments

Comments
 (0)