How to make only one data register on the Django admin screen

Purpose

I want to save data (for example, settings) that only needs one in total

Method

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

How to make only one data register on the Django admin screen
How to register the same data multiple times with one input on the Django management screen
How to filter foreign keys that can be selected on the Django admin screen
The story of failing to update "calendar.day_abbr" on the admin screen of django
How to add pre-save processing when adding objects on the Django admin site
How to register on pypi
django + nginx How to make images viewed only by logged-in users
How to return the data contained in django model in json format and map it on leaflet
How to check the version of Django
How to make a process thread run only on a specific CPU core
[2015/11/19] How to register a service locally using the python SDK on naoqi os
How to make multiple kernels selectable on Jupyter
[Django] How to get data by specifying SQL.
How to print debug messages to the Django console
I'm addicted to the difference in how Flask and Django receive JSON data
Implement the autocomplete feature on Django's admin screen
Customize the model page on Django's admin screen
Think about how to program Python on the iPad
How to apply markers only to specific data in matplotlib
How to put Takoyaki Oishikunaru on the segment tree
How to deploy a Django application on Alibaba Cloud
How to build a Django (python) environment on docker
Make the model a string on a Django HTML template
How to use Django on Google App Engine / Python
How to use Decorator in Django and how to make it
How to enjoy Python on Android !! Programming on the go !!
How to run Django on IIS on a Windows server
How to get only the data you need from a structured data set using a versatile method
How to write custom validations in the Django REST Framework
[Hyperledger Iroha] Notes on how to use the Python SDK
Don't lose to Ruby! How to run Python (Django) on Heroku
How to make a multiplayer online action game on Slack
How to check ORM behavior in one file with django
I want to make a blog editor with django admin
How to deploy the easiest python textbook pybot on Heroku
How to generate a query using the IN operator in Django
How to do the initial setup from Django project creation
I can't log in to the admin page with Django3
In Django, how to abbreviate the long displayed string as ....
Notes on how to use marshmallow in the schema library
DJango Memo: From the beginning (more edits to the management screen)
Django Changed to save lots of data in one go
[ROS] How to write Publisher and Subscriber on one node
How to print characters to the console before booting on ARM
[Introduction to Python] How to get data with the listdir function
How to use the generator
How to handle data frames
How to use the decorator
Django admin screen list_filter customization
How to increase the axis
How to make AWS Lambda Layers when running selenium × chrome on AWS Lambda
A note on how to check the connection to the license server port
How easy is it to synthesize a drug on the market?
Make the theme of Pythonista 3 like Monokai (how to make your own theme)
How to make a command to read the configuration file with pyramid
[Django] Correct the plural form of the model name on the management screen
How to make a .dylib library from a .a library on OSX (El Capitan)
I tried to register a station on the IoT platform "Rimotte"
Django shift table Shows only the facilities to which you belong
How to use Jupyter on the front end of supercomputer ITO
How to deploy a Django app on heroku in just 5 minutes