Enables support for primary keys that are not AutoFields.
Specially useful when using UUIDs as primary keys.master
parent
0e4a5b11e4
commit
3c2935776e
|
|
@ -1,5 +1,6 @@
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils import version
|
||||||
|
|
||||||
from adminsortable.fields import SortableForeignKey
|
from adminsortable.fields import SortableForeignKey
|
||||||
|
|
||||||
|
|
@ -87,7 +88,11 @@ class SortableMixin(models.Model):
|
||||||
'typecast to an integer.'
|
'typecast to an integer.'
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if not self.pk:
|
major, minor, _, _, _ = version.get_complete_version()
|
||||||
|
needs_default = (self._state.adding
|
||||||
|
if major == 1 and minor >= 8
|
||||||
|
else not self.pk)
|
||||||
|
if needs_default:
|
||||||
try:
|
try:
|
||||||
current_max = self.__class__.objects.aggregate(
|
current_max = self.__class__.objects.aggregate(
|
||||||
models.Max(self.order_field_name))[self.order_field_name + '__max'] or 0
|
models.Max(self.order_field_name))[self.order_field_name + '__max'] or 0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue