Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Properly handle PositionFields not named "position".

  • Loading branch information...
commit 1037ef711d9a8b585ab040ed866cb04daf40b18b 1 parent 2cffbf2
@jpwatts authored
Showing with 30 additions and 12 deletions.
  1. +25 −12 positions/examples/todo/models.py
  2. +5 −0 positions/managers.py
View
37 positions/examples/todo/models.py
@@ -5,9 +5,13 @@
class Item(models.Model):
description = models.CharField(max_length=50)
- position = positions.PositionField()
- objects = positions.PositionManager()
+ # I'm calling the PositionField "index" to make sure any internal code that
+ # relies on a PositionField being called "position" will break.
+ # https://github.com/jpwatts/django-positions/pull/12
+ index = positions.PositionField()
+
+ objects = positions.PositionManager('index')
def __unicode__(self):
return self.description
@@ -15,6 +19,12 @@ def __unicode__(self):
__test__ = {'API_TESTS':"""
+>>> Item.objects.position_field_name
+'index'
+
+>>> Item.objects.all().position_field_name
+'index'
+
>>> Item.objects.create(description="Add a `reposition` method")
<Item: Add a `reposition` method>
@@ -24,43 +34,46 @@ def __unicode__(self):
>>> Item.objects.create(description="Push to GitHub")
<Item: Push to GitHub>
->>> Item.objects.order_by('position')
+>>> Item.objects.order_by('index')
[<Item: Add a `reposition` method>, <Item: Write some tests>, <Item: Push to GitHub>]
>>> alphabetized = Item.objects.order_by('description')
>>> alphabetized
[<Item: Add a `reposition` method>, <Item: Push to GitHub>, <Item: Write some tests>]
+>>> alphabetized.position_field_name
+'index'
+
>>> repositioned = alphabetized.reposition(save=False)
>>> repositioned
[<Item: Add a `reposition` method>, <Item: Push to GitHub>, <Item: Write some tests>]
# Make sure the position wasn't saved
->>> Item.objects.order_by('position')
+>>> Item.objects.order_by('index')
[<Item: Add a `reposition` method>, <Item: Write some tests>, <Item: Push to GitHub>]
>>> repositioned = alphabetized.reposition()
>>> repositioned
[<Item: Add a `reposition` method>, <Item: Push to GitHub>, <Item: Write some tests>]
->>> Item.objects.order_by('position')
+>>> Item.objects.order_by('index')
[<Item: Add a `reposition` method>, <Item: Push to GitHub>, <Item: Write some tests>]
->>> item = Item.objects.order_by('position')[0]
+>>> item = Item.objects.order_by('index')[0]
>>> item
<Item: Add a `reposition` method>
->>> item.position
+>>> item.index
0
->>> item.position = -1
+>>> item.index = -1
>>> item.save()
# Make sure the signals are still connected
->>> Item.objects.order_by('position')
+>>> Item.objects.order_by('index')
[<Item: Push to GitHub>, <Item: Write some tests>, <Item: Add a `reposition` method>]
->>> [i.position for i in Item.objects.order_by('position')]
+>>> [i.index for i in Item.objects.order_by('index')]
[0, 1, 2]
@@ -68,10 +81,10 @@ def __unicode__(self):
# http://github.com/jpwatts/django-positions/issues/#issue/7
>>> item0 = Item(description="Fix Issue #7")
->>> item0.position = 0
+>>> item0.index = 0
>>> item0.save()
->>> Item.objects.values_list('description', 'position').order_by('position')
+>>> Item.objects.values_list('description', 'index').order_by('index')
[(u'Fix Issue #7', 0), (u'Push to GitHub', 1), (u'Write some tests', 2), (u'Add a `reposition` method', 3)]
"""}
View
5 positions/managers.py
@@ -10,6 +10,11 @@ def __init__(self, model=None, query=None, using=None, position_field_name='posi
super(PositionQuerySet, self).__init__(model, query, using)
self.position_field_name = position_field_name
+ def _clone(self, *args, **kwargs):
+ queryset = super(PositionQuerySet, self)._clone(*args, **kwargs)
+ queryset.position_field_name = self.position_field_name
+ return queryset
+
def reposition(self, save=True):
position_field = self.model._meta.get_field_by_name(self.position_field_name)[0]
post_save.disconnect(position_field.update_on_save, sender=self.model)
Please sign in to comment.
Something went wrong with that request. Please try again.