Handle 'next serial' more gracefully

This commit is contained in:
Ben Charlton 2020-08-21 19:17:58 +01:00
parent c31b30bf83
commit 23cc3d9b06
2 changed files with 6 additions and 3 deletions
InvenTree

View File

@ -14,6 +14,8 @@ from django.urls import reverse
from django.db import models, transaction from django.db import models, transaction
from django.db.models import Sum from django.db.models import Sum
from django.db.models.functions import Coalesce 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.core.validators import MinValueValidator
from django.contrib.auth.models import User from django.contrib.auth.models import User
@ -329,7 +331,8 @@ class Part(MPTTModel):
""" """
parts = Part.objects.filter(tree_id=self.tree_id) 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: if stock.count() > 0:
return stock.first().serial return stock.first().serial

View File

@ -391,8 +391,8 @@ class VariantTest(StockTest):
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
item.save() item.save()
# This should pass # This should pass, although not strictly an int field now.
item.serial = n + 1 item.serial = int(n) + 1
item.save() item.save()
# Attempt to create the same serial number but for a variant (should fail!) # Attempt to create the same serial number but for a variant (should fail!)