From 6cfa2fae3963c11e4c8ad180decba2928736dba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Thu, 29 May 2014 01:00:30 +0200 Subject: [PATCH 1/2] Fixed #22720 -- Migrations attempt to create _order twice. --- django/db/models/options.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/django/db/models/options.py b/django/db/models/options.py index a5f4280418..4f04746200 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -167,7 +167,8 @@ class Options(object): if self.order_with_respect_to: self.order_with_respect_to = self.get_field(self.order_with_respect_to) self.ordering = ('_order',) - model.add_to_class('_order', OrderWrt()) + if not any(isinstance(field, OrderWrt) for field in model._meta.local_fields): + model.add_to_class('_order', OrderWrt()) else: self.order_with_respect_to = None From bf9953cfb8d92f2757b6d5f0ba93d03533321619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Thu, 29 May 2014 01:38:59 +0200 Subject: [PATCH 2/2] Adding test to fix of duplicate _order fields (#22720) --- tests/order_with_respect_to/tests.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/order_with_respect_to/tests.py b/tests/order_with_respect_to/tests.py index 444caee142..2d5e85cb04 100644 --- a/tests/order_with_respect_to/tests.py +++ b/tests/order_with_respect_to/tests.py @@ -4,6 +4,8 @@ from operator import attrgetter from django.test import TestCase +from django.db import models + from .models import Post, Question, Answer @@ -71,3 +73,22 @@ class OrderWithRespectToTests(TestCase): Post.objects.create(title="2.1", parent=p2) p1_3 = Post.objects.create(title="1.3", parent=p1) self.assertEqual(p1.get_post_order(), [p1_1.pk, p1_2.pk, p1_3.pk]) + + def test_duplicate_order_field(self): + + class Bar(models.Model): + pass + + class Foo(models.Model): + bar = models.ForeignKey(Bar) + order = models.OrderWrt() + + class Meta: + order_with_respect_to = 'bar' + + count = 0 + for field in Foo._meta.local_fields: + if isinstance(field, models.OrderWrt): + count += 1 + + self.assertEqual(count, 1)