Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes #9 -- Allow the user to customize the name of the sort-by field…

… of the intermediate m2m relation table.
  • Loading branch information...
commit 2aaaf0316247fc945bf231baf50def6e04a8d4b2 1 parent 87756d0
@gregmuellegger authored
View
11 sortedm2m/fields.py
@@ -43,7 +43,7 @@ def set_managed(field, model, cls):
'auto_created': klass,
'app_label': klass._meta.app_label,
'unique_together': (from_, to),
- 'ordering': (SORT_VALUE_FIELD_NAME,),
+ 'ordering': (field.sort_value_field_name,),
'verbose_name': '%(from)s-%(to)s relationship' % {'from': from_, 'to': to},
'verbose_name_plural': '%(from)s-%(to)s relationships' % {'from': from_, 'to': to},
})
@@ -59,8 +59,8 @@ def default_sort_value(name):
'__module__': klass.__module__,
from_: models.ForeignKey(klass, related_name='%s+' % name),
to: models.ForeignKey(to_model, related_name='%s+' % name),
- SORT_VALUE_FIELD_NAME: models.IntegerField(default=default_sort_value),
- '_sort_field_name': SORT_VALUE_FIELD_NAME,
+ field.sort_value_field_name: models.IntegerField(default=default_sort_value),
+ '_sort_field_name': field.sort_value_field_name,
'_from_field_name': from_,
'_to_field_name': to,
})
@@ -174,6 +174,9 @@ class SortedManyToManyField(ManyToManyField):
'''
def __init__(self, to, sorted=True, **kwargs):
self.sorted = sorted
+ self.sort_value_field_name = kwargs.pop(
+ 'sort_value_field_name',
+ SORT_VALUE_FIELD_NAME)
super(SortedManyToManyField, self).__init__(to, **kwargs)
if self.sorted:
self.help_text = kwargs.get('help_text', None)
@@ -283,7 +286,7 @@ def forwards_code(self):
"right_field": self.field.m2m_reverse_name()[:-3], # Remove the _id part
"right_column": self.field.m2m_reverse_name(),
"right_model_key": model_key(self.field.rel.to),
- "sort_field": SORT_VALUE_FIELD_NAME,
+ "sort_field": self.field.sort_value_field_name,
}
else:
return super(AddM2M, self).forwards_code()
View
6 sortedm2m_tests/models.py
@@ -14,6 +14,12 @@ def __unicode__(self):
return self.name
+class DoItYourselfShelf(models.Model):
+ books = SortedManyToManyField(Book,
+ sort_value_field_name='diy_sort_number',
+ related_name='diy_shelves')
+
+
class Store(models.Model):
books = SortedManyToManyField('sortedm2m_tests.Book', related_name='stores')
View
29 sortedm2m_tests/sortedm2m_field/tests.py
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
+from django.db.models.fields import FieldDoesNotExist
from django.test import TestCase
-from sortedm2m_tests.models import Book, Shelf, Store, MessyStore, SelfReference
+from sortedm2m_tests.models import Book, Shelf, DoItYourselfShelf, Store, \
+ MessyStore, SelfReference
class TestSortedManyToManyField(TestCase):
@@ -161,6 +163,31 @@ class TestStringReference(TestSortedManyToManyField):
model = Store
+class TestStringReference(TestSortedManyToManyField):
+ '''
+ Test the same things as ``TestSortedManyToManyField`` but using a model
+ that using a string to reference the relation where the m2m field should
+ point to.
+ '''
+ model = DoItYourselfShelf
+
+ 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,
+ intermediate_model._meta.get_field_by_name,
+ SORT_VALUE_FIELD_NAME)
+
+ field = intermediate_model._meta.get_field_by_name('diy_sort_number')
+ self.assertTrue(field)
+
+
class TestSelfReference(TestCase):
def test_self_adding(self):
s1 = SelfReference.objects.create()
View
0  sortedm2m_tests/south_support/south_support_custom_sort_field_name/__init__.py
No changes.
View
20 sortedm2m_tests/south_support/south_support_custom_sort_field_name/migrations/0001_initial.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+ pass
+
+ def backwards(self, orm):
+ pass
+
+ models = {
+
+ }
+
+ complete_apps = ['south_support_custom_sort_field_name']
View
0  sortedm2m_tests/south_support/south_support_custom_sort_field_name/migrations/__init__.py
No changes.
View
14 sortedm2m_tests/south_support/south_support_custom_sort_field_name/models.py
@@ -0,0 +1,14 @@
+from django.db import models
+from ..models import Photo
+from sortedm2m.fields import SortedManyToManyField
+
+
+# this model is not considered in the existing migrations. South will try to
+# create this model.
+class FeaturedPhotos(models.Model):
+ name = models.CharField(max_length=30)
+ photos = SortedManyToManyField(Photo,
+ sort_value_field_name='featured_nr')
+
+ def __unicode__(self):
+ return self.name
View
17 sortedm2m_tests/south_support/tests.py
@@ -100,3 +100,20 @@ def test_new_field(self):
self.assertUnexpectedStrings([
"+ Added model south_support_new_field.PhotoStream",
], errput)
+
+ def test_custom_sort_field_name(self):
+ output, errput = self.perform_migration(
+ 'schemamigration',
+ 'south_support_custom_sort_field_name',
+ stdout=True,
+ auto=True)
+
+ self.assertExpectedStrings([
+ "Adding SortedM2M table for field photos on 'FeaturedPhotos'",
+ "('featured_nr', models.IntegerField())",
+ ], output)
+
+ self.assertExpectedStrings([
+ "+ Added model south_support_custom_sort_field_name.FeaturedPhotos",
+ "+ Added SortedM2M table for photos on south_support_custom_sort_field_name.FeaturedPhotos",
+ ], errput)
View
1  test_project/settings.py
@@ -79,6 +79,7 @@
'sortedm2m_tests.south_support',
'sortedm2m_tests.south_support.south_support_new_model',
'sortedm2m_tests.south_support.south_support_new_field',
+ 'sortedm2m_tests.south_support.south_support_custom_sort_field_name',
'example.testapp',
)
Please sign in to comment.
Something went wrong with that request. Please try again.