From 181528726ed2e3f1457e13872c777d0447cc52b8 Mon Sep 17 00:00:00 2001 From: Kai Richard Koenig Date: Wed, 1 Jul 2015 09:21:17 +0200 Subject: [PATCH] [1.8.x] Fixed #25040 -- Fixed migrations state crash with GenericForeignKey Backport of 60f795c0608119cdb02d61d3eb59f34ebdb55754 from master --- django/db/migrations/state.py | 2 +- docs/releases/1.8.5.txt | 2 ++ tests/migrations/test_state.py | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/django/db/migrations/state.py b/django/db/migrations/state.py index 8add0b3db1..c01b12ff16 100644 --- a/django/db/migrations/state.py +++ b/django/db/migrations/state.py @@ -45,7 +45,7 @@ def get_related_models_recursive(model): def _related_models(m): return [ f.related_model for f in m._meta.get_fields(include_parents=True, include_hidden=True) - if f.is_relation and not isinstance(f.related_model, six.string_types) + if f.is_relation and f.related_model is not None and not isinstance(f.related_model, six.string_types) ] + [ subclass for subclass in m.__subclasses__() if issubclass(subclass, models.Model) diff --git a/docs/releases/1.8.5.txt b/docs/releases/1.8.5.txt index df7783f4df..d1e75d97d6 100644 --- a/docs/releases/1.8.5.txt +++ b/docs/releases/1.8.5.txt @@ -11,3 +11,5 @@ Bugfixes * Fixed ``AssertionError`` in some delete queries with a model containing a field that is both a foreign and primary key (:ticket:`24951`). + +* Fixed a migrations crash with ``GenericForeignKey`` (:ticket:`25040`). diff --git a/tests/migrations/test_state.py b/tests/migrations/test_state.py index 1fdb305e41..87c89db899 100644 --- a/tests/migrations/test_state.py +++ b/tests/migrations/test_state.py @@ -1,4 +1,5 @@ from django.apps.registry import Apps +from django.contrib.contenttypes.fields import GenericForeignKey from django.db import models from django.db.migrations.operations import ( AddField, AlterField, DeleteModel, RemoveField, @@ -976,6 +977,12 @@ class RelatedModelsTests(SimpleTestCase): self.assertRelated(S, [A, B, T]) self.assertRelated(T, [A, B, S]) + def test_generic_fk(self): + A = self.create_model("A", foreign_keys=[models.ForeignKey('B'), GenericForeignKey()]) + B = self.create_model("B", foreign_keys=[models.ForeignKey('C')]) + self.assertRelated(A, [B]) + self.assertRelated(B, [A]) + def test_abstract_base(self): A = self.create_model("A", abstract=True) B = self.create_model("B", bases=(A,))