@@ -63,12 +63,12 @@ def save_swap_objects(self, sender, instance, created, **kwargs):
63
63
64
64
def get_swap_objects (self , sender , instance , ** kwargs ):
65
65
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 :
72
72
predecessor = self ._get_predecessor (instance , new_value )
73
73
if predecessor :
74
74
_set_swap_integers (instance , predecessor , swap_value , column )
@@ -84,7 +84,7 @@ def _get_swap_value(self, instance):
84
84
def _get_old_value (self , instance ):
85
85
if instance .pk :
86
86
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
88
88
return None
89
89
90
90
def _get_common_filter (self , instance ):
@@ -101,9 +101,9 @@ def _get_next_available_value(self, instance):
101
101
queryset = self .model .objects .filter (** query_filter )
102
102
if queryset .count ():
103
103
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
105
106
return None
106
- # return queryset.aggregate(Max(self.column)) + 1
107
107
108
108
def _get_predecessor (self , instance , data ):
109
109
query_filter = self ._get_common_filter (instance )
0 commit comments