Skip to content
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
39 changes: 34 additions & 5 deletions api/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from django.contrib import admin
from django import forms
from django.db import connection, IntegrityError


from api.models import (
AIModel,
Expand Down Expand Up @@ -219,9 +222,9 @@ class PromptResourceAdmin(admin.ModelAdmin):
search_fields = ("resource__name",)


# ---------------------------------------------------------------------------
# AIModel & related
# ---------------------------------------------------------------------------
class AIModelForm(forms.ModelForm):
id = forms.IntegerField(required=False) # makes id editable


class ModelEndpointInline(admin.TabularInline):
model = ModelEndpoint
Expand All @@ -231,7 +234,9 @@ class ModelEndpointInline(admin.TabularInline):

@admin.register(AIModel)
class AIModelAdmin(admin.ModelAdmin):
form = AIModelForm
list_display = (
"id",
"display_name",
"name",
"provider",
Expand All @@ -256,7 +261,7 @@ class AIModelAdmin(admin.ModelAdmin):
fieldsets = (
(
"Basic Information",
{"fields": ("name", "display_name", "version", "description")},
{"fields": ("id", "name", "display_name", "version", "description")}, # ← id added here
),
(
"Model Configuration",
Expand Down Expand Up @@ -294,7 +299,31 @@ class AIModelAdmin(admin.ModelAdmin):
),
)


def save_model(self, request, obj, form, change):
"""
Allow changing the primary key ('id') by running a raw UPDATE.
"""
if change and 'id' in form.changed_data:
old_pk = form.initial['id'] # the id when the form was opened
new_pk = form.cleaned_data['id'] # the new id the user entered

# Prevent duplicate key error – check if new_pk already exists
if AIModel.objects.filter(pk=new_pk).exclude(pk=old_pk).exists():
raise IntegrityError(f"Primary key {new_pk} already exists.")

with connection.cursor() as cursor:
cursor.execute(
f"UPDATE {AIModel._meta.db_table} SET id = %s WHERE id = %s",
[new_pk, old_pk]
)
# Update the in‑memory object’s pk so the admin redirect works correctly
obj.pk = new_pk
# No need to call save() again – the update is already applied.
else:
# Normal save (create or update without pk change)
super().save_model(request, obj, form, change)


@admin.register(AIModelVersion)
class AIModelVersionAdmin(admin.ModelAdmin):
list_display = ("ai_model", "version", "status", "created_at")
Expand Down
10 changes: 5 additions & 5 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ python manage.py makemigrations --noinput
echo "Running migrations..."
python manage.py migrate --noinput

# Create superuser if needed
if [ "$DJANGO_SUPERUSER_USERNAME" ] && [ "$DJANGO_SUPERUSER_PASSWORD" ] && [ "$DJANGO_SUPERUSER_EMAIL" ]; then
echo "Creating superuser..."
python manage.py createsuperuser --noinput
# Create superuser if needed (ignore failure, e.g. if already exists)
if [ -n "$DJANGO_SUPERUSER_USERNAME" ] && [ -n "$DJANGO_SUPERUSER_PASSWORD" ] && [ -n "$DJANGO_SUPERUSER_EMAIL" ]; then
echo "Creating superuser (if not exists)..."
python manage.py createsuperuser --noinput 2>/dev/null || echo "Superuser already exists or creation skipped"
fi

# Collect static files
Expand All @@ -67,4 +67,4 @@ fi

# Start server
echo "Starting server..."
exec "$@"
exec "$@"
Loading