I want to save data (for example, settings) that only needs one in total
Model
models.py
from django.db import models
class Setting(models.Model):
pass
Admin
admin.py
from django.contrib import admin
from .models import Setting
class SettingAdmin(admin.ModelAdmin):
def has_add_permission(self, request):
#Allow only one setting to be registered
count = Setting.objects.all().count()
if count == 0:
return True
return False
def has_delete_permission(self, request, obj=None):
#Prevent settings from being deleted
return False
admin.site.register(Setting,SettingAdmin)
By overriding has_add_permission, you can control the number of registrations and the inability to add per user from request.user. Similarly, you can override has_delete_permission to control deletion.
Recommended Posts