Skip to content

Commit 87e0271

Browse files
committed
Also support db_column attribute
1 parent 8b22681 commit 87e0271

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

simple_history/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ def copy_fields(self, model):
148148
field_arguments['to_field'] = old_field.to_fields[0]
149149
elif django.get_version() < "1.6" and old_field.rel.field_name != 'id':
150150
field_arguments['to_field'] = old_field.rel.field_name
151+
if getattr(old_field, 'db_column', None):
152+
field_arguments['db_column'] = old_field.db_column
151153
field = FieldType(
152154
old_field.rel.to,
153155
related_name='+',

simple_history/tests/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,8 @@ class Country(models.Model):
259259
class Province(models.Model):
260260
country = models.ForeignKey(Country, to_field='code')
261261
history = HistoricalRecords()
262+
263+
264+
class City(models.Model):
265+
country = models.ForeignKey(Country, db_column='countryCode')
266+
history = HistoricalRecords()

simple_history/tests/tests/test_models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
AbstractBase, ConcreteAttr, ConcreteUtil, SelfFK, Temperature, WaterLevel,
1919
ExternalModel1, ExternalModel3, UnicodeVerboseName, HistoricalChoice,
2020
HistoricalState, HistoricalCustomFKError, Series, SeriesWork, PollInfo,
21-
UserAccessorDefault, UserAccessorOverride, Employee, Country, Province
21+
UserAccessorDefault, UserAccessorOverride, Employee, Country, Province,
22+
City
2223
)
2324
from ..external.models import ExternalModel2, ExternalModel4
2425

@@ -266,6 +267,12 @@ def test_to_field_foreign_key_save(self):
266267
self.assertEqual([c.country_id for c in province.history.all()],
267268
[country2.code, country.code])
268269

270+
def test_db_column_foreign_key_save(self):
271+
country = Country.objects.create(code='US')
272+
city = City.objects.create(country=country)
273+
country_field = City._meta.get_field('country')
274+
self.assertTrue(getattr(country_field, 'db_column') in str(city.history.all().query))
275+
269276
def test_raw_save(self):
270277
document = Document()
271278
document.save_base(raw=True)

0 commit comments

Comments
 (0)