Hide default permission in Django admin form

500 views
Skip to first unread message

Robert Jonathan Šimon

unread,
May 6, 2014, 6:45:28 AM5/6/14
to django...@googlegroups.com
I looked up the answer (http://stackoverflow.com/questions/6062655/remove-or-hide-default-permissions-from-django), it worked, but suddenly it stopped working, i am not sure, what i did or if I reinstaled something. I have this solution:

from django.contrib import admin
from django.contrib.auth.models import Permission
from django.contrib.auth.models import User, Group
from django.contrib.auth.admin import GroupAdmin, UserAdmin
from django.contrib.auth.forms import UserChangeForm

#
# In the models listed below standard permissions "add_model", "change_model"
# and "delete_model" will be created by syncdb, but hidden from admin interface.
# This is convenient in case you use your own set of permissions so the list
# in the admin interface wont be confusing.
# Feel free to add your models here. The first element is the app name (this is
# the directory your app is in) and the second element is the name of your model
# from models.py module of your app (Note: both names must be lowercased).
#
MODELS_TO_HIDE_STD_PERMISSIONS = (
    ("auth", "permission"),
    ("auth", "group"),
    ("auth", "user"),
    ("contenttypes", "contenttype"),
    ("sessions", "session"),
    ("sites", "site"),
    ("admin", "logentry"),
    ("mainpage","novinkymodel"),
    ("mainpage", "komentarknovinkymodel"),
    ("mainpage", "clovek"),
    ("mainpage", "ucitel"),
    ("mainpage", "trida"),
    ("mainpage", "predmety"),
    ("mainpage", "administrator"),
    ("mainpage", "student"),
    ("mainpage", "rodic"),
    ("kalendar", "udalost"),
    ("fotogalerie", "slozka"),
    ("fotogalerie", "fotka"),
    ("south", "migrationhistory")
    )

def _get_corrected_permissions():
    perms = Permission.objects.all()
    for app_name, model_name in MODELS_TO_HIDE_STD_PERMISSIONS:
        perms = perms.exclude(content_type__app_label=app_name, codename='add_%s' % model_name)
        perms = perms.exclude(content_type__app_label=app_name, codename='change_%s' % model_name)
        perms = perms.exclude(content_type__app_label=app_name, codename='delete_%s' % model_name)
    return perms

class MyGroupAdminForm(forms.ModelForm):

    class Meta:
        model = Group

    permissions = forms.ModelMultipleChoiceField(
        _get_corrected_permissions(),
        widget=admin.widgets.FilteredSelectMultiple(('permissions'), False),
        help_text = 'Hold down "Control", or "Command" on a Mac, to select more than one.'
    )

class MyGroupAdmin(GroupAdmin):

    form = MyGroupAdminForm

class MyUserChangeForm(UserChangeForm):

    user_permissions = forms.ModelMultipleChoiceField(
        _get_corrected_permissions(),
        widget=admin.widgets.FilteredSelectMultiple(('user_permissions'), False),
        help_text = 'Hold down "Control", or "Command" on a Mac, to select more than one.'
    )

class MyUserAdmin(UserAdmin):

    form = MyUserChangeForm

admin.site.unregister(Group)
admin.site.register(Group, MyGroupAdmin)
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)


I am using Django 1.6.4 and Python 3.4. This code is in my app and file admin.py

Robert Jonathan Šimon

unread,
May 7, 2014, 3:21:06 AM5/7/14
to django...@googlegroups.com
i know where is problem, but i dont  know how can I solve it. Admin site isnt registering this models. In this zip its Django site, where it not working. Does anyone know where is problem please?

Dne úterý, 6. května 2014 12:45:28 UTC+2 Robert Jonathan Šimon napsal(a):
mysite.zip
Reply all
Reply to author
Forward
0 new messages