Skip to content

Commit e406c11

Browse files
committed
fix conditions when value is None (v0.1.1 if is OK)
1 parent 549e502 commit e406c11

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name='django-swapfield',
9-
version='0.1.0',
9+
version='0.1.1',
1010
packages=find_packages(),
1111
url='https://github.com/SRJ9/django-swapfield.git',
1212
download_url='https://github.com/SRJ9/django-swapfield/archive/master.zip',

swapfield/fields.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ def save_swap_objects(self, sender, instance, created, **kwargs):
6363

6464
def get_swap_objects(self, sender, instance, **kwargs):
6565
column = self.column
66-
# We need the value of field saved in database or the next available
67-
# to swap with the object that contains the new value.
68-
swap_value = self._get_swap_value(instance)
69-
if swap_value:
70-
new_value = getattr(instance, column)
71-
if new_value != swap_value:
66+
new_value = getattr(instance, column)
67+
if new_value:
68+
# We need the value of field saved in database or the next available
69+
# to swap with the object that contains the new value.
70+
swap_value = self._get_swap_value(instance)
71+
if swap_value and new_value != swap_value:
7272
predecessor = self._get_predecessor(instance, new_value)
7373
if predecessor:
7474
_set_swap_integers(instance, predecessor, swap_value, column)
@@ -84,7 +84,7 @@ def _get_swap_value(self, instance):
8484
def _get_old_value(self, instance):
8585
if instance.pk:
8686
old_instance = self.model.objects.filter(pk=instance.pk).first()
87-
return getattr(old_instance, self.column)
87+
return getattr(old_instance, self.column) if old_instance else None
8888
return None
8989

9090
def _get_common_filter(self, instance):
@@ -101,9 +101,9 @@ def _get_next_available_value(self, instance):
101101
queryset = self.model.objects.filter(**query_filter)
102102
if queryset.count():
103103
aggregate = queryset.aggregate(value__max=Max(self.column))
104-
return aggregate.get('value__max') + 1
104+
value_max = aggregate.get('value__max')
105+
return value_max + 1 if value_max else None
105106
return None
106-
# return queryset.aggregate(Max(self.column)) + 1
107107

108108
def _get_predecessor(self, instance, data):
109109
query_filter = self._get_common_filter(instance)

0 commit comments

Comments
 (0)