Login

One register on admin

Author:
zodman
Posted:
February 23, 2012
Language:
Python
Version:
1.3
Tags:
admin no-add no-delete one-line one-row one-register
Score:
1 (after 1 ratings)

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

  1. Add delete button in admin cp by cschand 7 years, 3 months ago
  2. easy admin registration by alia_khouri 6 years, 10 months ago
  3. Accordion changelist admin by rodnsi 5 years, 7 months ago
  4. Generic admin action export selected rows to excel by jordic 3 years, 9 months ago
  5. Form with Two InlineFormSets by maeck 6 years, 6 months ago

Comments

Please login first before commenting.