Merge pull request #153 from rubendura/master
Enables support for primary keys that are not AutoFields.master
commit
c2ca9db931
|
|
@ -10,3 +10,5 @@ atlassian-*
|
||||||
.ropeproject
|
.ropeproject
|
||||||
.codeintel
|
.codeintel
|
||||||
__pycache__
|
__pycache__
|
||||||
|
.venv/
|
||||||
|
build
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ except ImportError:
|
||||||
import http.client as httplib
|
import http.client as httplib
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import uuid
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
@ -27,6 +28,14 @@ class TestSortableModel(SortableMixin):
|
||||||
return self.title
|
return self.title
|
||||||
|
|
||||||
|
|
||||||
|
class TestNonAutoFieldModel(SortableMixin):
|
||||||
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||||
|
order = models.PositiveIntegerField(editable=False, db_index=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
ordering = ['order']
|
||||||
|
|
||||||
|
|
||||||
class SortableTestCase(TestCase):
|
class SortableTestCase(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.client = Client()
|
self.client = Client()
|
||||||
|
|
@ -325,3 +334,7 @@ class SortableTestCase(TestCase):
|
||||||
]
|
]
|
||||||
self.assertEqual(notes, expected_notes)
|
self.assertEqual(notes, expected_notes)
|
||||||
|
|
||||||
|
def test_save_non_auto_field_model(self):
|
||||||
|
model = TestNonAutoFieldModel()
|
||||||
|
model.save()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue