jpwatts / django-positions

A Django field for custom model ordering.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Potential Bug with Turning a Parent Node into a Child Node

g-cassie opened this issue · comments

Hi,
I was hoping to try and write a test to show this bug but I am not sure how to run the tests in the examples. If you let me know I can submit a PR with a test replicating the bug under the nodes example app.

Here is the bug.

Using this model (slightly simplified from my actual model):

class Category(models.Model):
    title = models.CharField(max_length=200)
    position = PositionField(collection='parent_category')
    parent_category = models.ForeignKey('self',
                                        related_name='subcategories',
                                        blank=True,
                                        null=True,
                                        on_delete=models.SET_NULL)

Running this test:

    def test_category_positions_bug(self):
        category = Category.objects.create(title='First Category')
        category.parent_category = Category.objects.create(title='Second Category')
        category.save()

Causes this error:

======================================================================
ERROR: test_category_positions_bug (deals.tests.CategoryTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/gordon--c/Sites/sample_project/sample_app/tests.py", line 274, in test_category_positions_bug
    category.save()
  File "/Users/gordon--c/virtualenvs/sample/lib/python2.7/site-packages/django/db/models/base.py", line 546, in save
    force_update=force_update, update_fields=update_fields)
  File "/Users/gordon--c/virtualenvs/sample/lib/python2.7/site-packages/django/db/models/base.py", line 624, in save_base
    values = [(f, None, (raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks]
  File "/Users/gordon--c/virtualenvs/sample/lib/python2.7/site-packages/positions/fields.py", line 112, in pre_save
    elif abs(updated) <= (max_position + 1):
TypeError: bad operand type for abs(): 'NoneType'

Thanks for the bug report and especially for offering to write the test! I run the tests in the examples using the --settings option of the django-admin.py script that ships with Django.

$ django-admin.py test --settings=positions.examples.settings

Closing so discussion can be continued in #23