Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set explicit PK when creating a new Request #8

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions backend/request/admin.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion backend/request/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down