treyhunner/django-simple-history — pull request #61 by foobacca (2013-07-25 15:43:25+00:00)
Title: Fix disappearing reverse relationship (ie modelname_set ) from model with history

We found that adding history to a model with a foreign key resulted
in the reverse relationship disappearing (ie modelname_set no longer
existed).

The problem is that a shallow copy of the field is made, and when the
related name is updated by:

field.rel.related_name = '+'

this updates the related name of the field in the original model.

Note that the modelname_set attribute is only not generated when the
model with the ForeignKey is declared before the model that is the
target of the ForeignKey (and therefore the ForeignKey is declared using
a string).

This means the bug isn't obvious as the order of model declaration
determines whether the bug arises.

The fix is to make a deeper copy of the field, so that when:

field.rel.related_name = '+'

is run, this only changes the local copy, not the related name of the
field in the original model.