def test_operation_m2m_to_sorted_m2m(self):

        # Let's start with state after 0001
        with capture_stdout():
            call_command('migrate', 'altersortedmanytomanyfield_tests', '0001')

        Target = self.state_0001_apps.get_model(
            'altersortedmanytomanyfield_tests', 'target')
        M2MToSortedM2M = self.state_0001_apps.get_model(
            'altersortedmanytomanyfield_tests', 'm2mtosortedm2m')

        t1 = Target.objects.create(pk=1)
        t2 = Target.objects.create(pk=2)
        t3 = Target.objects.create(pk=3)

        field = get_field(M2MToSortedM2M,'m2m')
        through_model = get_rel(field).through
        # No ordering is in place.
        self.assertTrue(not through_model._meta.ordering)


        instance = M2MToSortedM2M.objects.create(pk=1)
        instance.m2m.add(t3)
        instance.m2m.add(t1)
        instance.m2m.add(t2)

        # We cannot assume any particular order now.

        # Migrate to state 0002, then we should be able to apply order.
        with capture_stdout():
            call_command('migrate', 'altersortedmanytomanyfield_tests', '0002')

        Target = self.state_0002_apps.get_model(
            'altersortedmanytomanyfield_tests', 'target')
        M2MToSortedM2M = self.state_0002_apps.get_model(
            'altersortedmanytomanyfield_tests', 'm2mtosortedm2m')

        t1 = Target.objects.get(pk=1)
        t2 = Target.objects.get(pk=2)
        t3 = Target.objects.get(pk=3)

        field = get_field(M2MToSortedM2M,'m2m')
        through_model = get_rel(field).through
        # Now, ordering is there.
        self.assertTrue(list(through_model._meta.ordering), ['sort_value'])

        instance = M2MToSortedM2M.objects.get(pk=1)
        self.assertEqual(list(instance.m2m.order_by('pk')), [t1, t2, t3])

        m2m_set(instance, "m2m", [t3, t1, t2])

        self.assertEqual(list(instance.m2m.all()), [t3, t1, t2])

        instance.m2m.remove(t1)
        instance.m2m.remove(t2)
        instance.m2m.add(t2)
        instance.m2m.add(t1)

        self.assertEqual(list(instance.m2m.all()), [t3, t2, t1])
    def test_operation_m2m_to_sorted_m2m(self):

        # Let's start with state after 0001
        with capture_stdout():
            call_command('migrate', 'altersortedmanytomanyfield_tests', '0001')

        Target = self.state_0001_apps.get_model(
            'altersortedmanytomanyfield_tests', 'target')
        M2MToSortedM2M = self.state_0001_apps.get_model(
            'altersortedmanytomanyfield_tests', 'm2mtosortedm2m')

        t1 = Target.objects.create(pk=1)
        t2 = Target.objects.create(pk=2)
        t3 = Target.objects.create(pk=3)

        field = get_field(M2MToSortedM2M,'m2m')
        through_model = field.rel.through
        # No ordering is in place.
        self.assertTrue(not through_model._meta.ordering)


        instance = M2MToSortedM2M.objects.create(pk=1)
        instance.m2m.add(t3)
        instance.m2m.add(t1)
        instance.m2m.add(t2)

        # We cannot assume any particular order now.

        # Migrate to state 0002, then we should be able to apply order.
        with capture_stdout():
            call_command('migrate', 'altersortedmanytomanyfield_tests', '0002')

        Target = self.state_0002_apps.get_model(
            'altersortedmanytomanyfield_tests', 'target')
        M2MToSortedM2M = self.state_0002_apps.get_model(
            'altersortedmanytomanyfield_tests', 'm2mtosortedm2m')

        t1 = Target.objects.get(pk=1)
        t2 = Target.objects.get(pk=2)
        t3 = Target.objects.get(pk=3)

        field = get_field(M2MToSortedM2M,'m2m')
        through_model = field.rel.through
        # Now, ordering is there.
        self.assertTrue(list(through_model._meta.ordering), ['sort_value'])

        instance = M2MToSortedM2M.objects.get(pk=1)
        self.assertEqual(list(instance.m2m.order_by('pk')), [t1, t2, t3])

        instance.m2m = [t3, t1, t2]

        self.assertEqual(list(instance.m2m.all()), [t3, t1, t2])

        instance.m2m.remove(t1)
        instance.m2m.remove(t2)
        instance.m2m.add(t2)
        instance.m2m.add(t1)

        self.assertEqual(list(instance.m2m.all()), [t3, t2, t1])
    def test_custom_sort_value_field_name(self):
        from sortedm2m.fields import SORT_VALUE_FIELD_NAME

        self.assertEqual(len(self.model._meta.many_to_many), 1)
        sortedm2m = self.model._meta.many_to_many[0]
        intermediate_model = sortedm2m.rel.through

        # make sure that standard sort field is not used
        self.assertRaises(FieldDoesNotExist, get_field, intermediate_model,
                          SORT_VALUE_FIELD_NAME)

        field = get_field(intermediate_model, 'diy_sort_number')
        self.assertTrue(field)
    def test_custom_sort_value_field_name(self):
        from sortedm2m.fields import SORT_VALUE_FIELD_NAME

        self.assertEqual(len(self.model._meta.many_to_many), 1)
        sortedm2m = self.model._meta.many_to_many[0]
        intermediate_model = sortedm2m.rel.through

        # make sure that standard sort field is not used
        self.assertRaises(
            FieldDoesNotExist,
            get_field,
            intermediate_model, SORT_VALUE_FIELD_NAME)

        field = get_field(intermediate_model, 'diy_sort_number')
        self.assertTrue(field)