from django.contrib import admin from django import forms from .models import Neo4jProfile class Neo4jProfileForm(forms.ModelForm): """ Simplified form to ensure passwords are always saved correctly. """ password = forms.CharField( widget=forms.PasswordInput(render_value=True), required=True, # 🔹 Make password required to ensure it's always saved help_text="Enter the Neo4j password." ) openai_api_key = forms.CharField( widget=forms.PasswordInput(render_value=True), required=True, # 🔹 Make API key required for consistency help_text="Enter the OpenAI API key." ) class Meta: model = Neo4jProfile fields = "__all__" @admin.register(Neo4jProfile) class Neo4jProfileAdmin(admin.ModelAdmin): """ Simplified admin configuration ensuring password is always stored. """ form = Neo4jProfileForm list_display = ("name", "uri", "model_name") # Display only non-sensitive info search_fields = ("name", "uri", "model_name") # Enable search functionality list_filter = ("model_name",) # Allow filtering by AI model def save_model(self, request, obj, form, change): """ Ensures the password is always saved. """ obj.password = form.cleaned_data["password"] # 🔹 Always save password explicitly obj.openai_api_key = form.cleaned_data["openai_api_key"] # 🔹 Always save API key explicitly super().save_model(request, obj, form, change)