diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index cea78543c6..fba217e0d9 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -14,6 +14,8 @@ from django.urls import reverse from django.db import models, transaction from django.db.models import Sum from django.db.models.functions import Coalesce +from django.db.models import IntegerField +from django.db.models.functions import Cast from django.core.validators import MinValueValidator from django.contrib.auth.models import User @@ -329,7 +331,8 @@ class Part(MPTTModel): """ parts = Part.objects.filter(tree_id=self.tree_id) - stock = StockModels.StockItem.objects.filter(part__in=parts).exclude(serial=None).order_by('-serial') + stock = StockModels.StockItem.objects.filter(part__in=parts).exclude(serial=None).annotate( + serial_as_int=Cast('serial', output_field=IntegerField())).order_by('-serial_as_int') if stock.count() > 0: return stock.first().serial diff --git a/InvenTree/stock/tests.py b/InvenTree/stock/tests.py index 03a04b73d8..5442584b7d 100644 --- a/InvenTree/stock/tests.py +++ b/InvenTree/stock/tests.py @@ -391,8 +391,8 @@ class VariantTest(StockTest): with self.assertRaises(ValidationError): item.save() - # This should pass - item.serial = n + 1 + # This should pass, although not strictly an int field now. + item.serial = int(n) + 1 item.save() # Attempt to create the same serial number but for a variant (should fail!)