This code is for set one register on admin if exist more than 1 register you can not delete it or add more.
Only set mymodel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | from django.contrib import admin
from .models import Section, Package, Instruction, ActivePackages
class Restricted(admin.ModelAdmin):
actions = None
mymodel = None
def has_add_permission(self, request):
assert self.mymodel is not None, "No set mymodel var"
if self.mymodel.objects.count() >= 1:
return False
else:
return True
def has_delete_permission(self, request, obj =None):
return False
class SeccionAdmin(Restricted):
mymodel = Section
class ActivePackagesAdmin(Restricted):
mymodel = ActivePackages
admin.site.register(Section, SeccionAdmin)
admin.site.register(ActivePackages, ActivePackagesAdmin)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 1 year ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year ago
- Serializer factory with Django Rest Framework by julio 1 year, 7 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 8 months ago
- Help text hyperlinks by sa2812 1 year, 9 months ago
Comments
Please login first before commenting.