Fixes #368.

Integrates @mivanov's tests and fixes the related_name issue with
non-versioned FK models.