diff --git a/backend/request/admin.py b/backend/request/admin.py index a96447a0d..5091f6617 100644 --- a/backend/request/admin.py +++ b/backend/request/admin.py @@ -1,3 +1,4 @@ +from typing import Any from common.admin import ArchivedFilter from django.contrib import admin from django_admin_listfilter_dropdown.filters import RelatedDropdownFilter @@ -53,6 +54,18 @@ class RequestAdmin(admin.ModelAdmin): "mark_as_non_archived", ) + def save_model(self, request, obj, form, change): + + # Set explicit PK when creating a new Request, do not rely on the + # DB autoincrement value because it does not know what the last + # actual Request ID in the DB is + # NZ, disable/amend this if you need to use an ID other than the + # next integer after the largest Request ID in the DB + if obj.pk == None and Request.objects.exists(): + obj.id = Request.objects.order_by('-id').first().id + 1 + + return super().save_model(request, obj, form, change) + @admin.action(description="Mark as archived") def mark_as_archived(self, request, queryset): queryset.update(archived=True) diff --git a/backend/request/serializers.py b/backend/request/serializers.py index 107ce7752..308f84762 100644 --- a/backend/request/serializers.py +++ b/backend/request/serializers.py @@ -180,8 +180,16 @@ def rename_files(self, instance): def create(self, validated_data): + # Set explicit PK when creating a new Request, do not rely on the + # DB autoincrement value because it does not know what the last + # actual Request ID in the DB is + # NZ, disable/amend this if you need to use an ID other than the + # next integer after the largest Request ID in the DB + if Request.objects.exists(): + validated_data.update({'pk': Request.objects.order_by('-id').first().id + 1}) + instance = super().create(validated_data) - + self.rename_files(instance) return instance