Login

One register on admin

Author:
zodman
Posted:
February 23, 2012
Language:
Python
Version:
1.3
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. Template tag - list punctuation for a list of items by shapiromatron 5 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 5 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year ago
  5. Help text hyperlinks by sa2812 1 year, 1 month ago

Comments

Please login first before commenting.