diff --git a/django/db/migrations/state.py b/django/db/migrations/state.py index 275d3b03a4..0134acde72 100644 --- a/django/db/migrations/state.py +++ b/django/db/migrations/state.py @@ -443,6 +443,9 @@ class ModelState(object): for key in ["unique_together", "index_together", "order_with_respect_to"]: if key in options: del options[key] + # Private fields are ignored, so remove options that refer to them. + elif options.get('order_with_respect_to') in {field.name for field in model._meta.private_fields}: + del options['order_with_respect_to'] def flatten_bases(model): bases = [] diff --git a/tests/migrations/test_state.py b/tests/migrations/test_state.py index 531ec2e131..aae2b71aac 100644 --- a/tests/migrations/test_state.py +++ b/tests/migrations/test_state.py @@ -9,6 +9,7 @@ from django.db.migrations.state import ( ModelState, ProjectState, get_related_models_recursive, ) from django.test import SimpleTestCase, override_settings +from django.test.utils import isolate_apps from django.utils import six from .models import ( @@ -911,6 +912,19 @@ class ModelStateTests(SimpleTestCase): self.assertEqual([name for name, mgr in food_state.managers], ['food_mgr']) self.assertEqual(food_state.managers[0][1].args, ('a', 'b', 1, 2)) + @isolate_apps('migrations', 'django.contrib.contenttypes') + def test_order_with_respect_to_private_field(self): + class PrivateFieldModel(models.Model): + content_type = models.ForeignKey('contenttypes.ContentType', models.CASCADE) + object_id = models.PositiveIntegerField() + private = GenericForeignKey() + + class Meta: + order_with_respect_to = 'private' + + state = ModelState.from_model(PrivateFieldModel) + self.assertNotIn('order_with_respect_to', state.options) + class RelatedModelsTests(SimpleTestCase):