Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding custom permission through django-admin, while server is running

In Django-admin, Is it possible to make feature so that admin can create/edit/delete certain permission through django-admin while the server is running?

In django-admin I want the permission can be listed, with edit create and delete feature

Using permission in Meta subclass of models class will create custom permission through migrate script. taken from https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#custom-permissions

class Task(models.Model):
...
    class Meta:
        permissions = (
            ("view_task", "Can see available tasks"),
            ("change_task_status", "Can change the status of tasks"),
            ("close_task", "Can remove a task by setting its status as closed"),
        )

This will insert values on auth_permission (and django_content_type) But that requires database migrations, which means unlikely be done by user (Admin) but by the developer instead. And I believe it is necessary for application to be able to manage permission, creating, editing, deleting while the server is running.

So what is the best practice to make this functionality with Django? Should I keep using migrate & create them all in each model and to reload server everytime we implement new permission or feature in general? thankyou

like image 985
izzulmakin Avatar asked Sep 02 '15 11:09

izzulmakin


People also ask

How do I add custom permissions to Django?

Django Admin Panel : In Admin Panel you will see Group in bold letter, Click on that and make 3-different group named level0, level1, level3 . Also, define the custom permissions according to the need. By Programmatically creating a group with permissions: Open python shell using python manage.py shell.

How do I give permission to a specific user in Django?

through django-adminopen your django-admin page and head to Users section and select your desired user . NOTE: Permission assigning process is a one-time thing, so you dont have to update it every time unless you need to change/re-assign the permissions.

Is Django's admin interface customizable if yes then how?

To implement it in your project, make a new app in your Django project named products. Install this application, type product in the INSTALLED_APPS list in settings.py file. We will now make models in the products app. The model will be used throughout the tutorial to customize the Django Admin.


2 Answers

You also need to select_related because it will result in a bunch of SQL queries

from django.contrib import admin
from django.contrib.auth.models import Permission


@admin.register(Permission)
class PermissionAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.select_related('content_type')
like image 151
Artem Bernatskyi Avatar answered Oct 13 '22 19:10

Artem Bernatskyi


You can register the Permission model to admin view:

from django.contrib.auth.models import Permission
from django.contrib import admin
admin.site.register(Permission)

The code can be anywhere where it gets executed, but admin.py of your application could be an intuitive place to stick it in. After this, you will be able to view, edit and remove the permissions.

like image 35
Juuso Meriläinen Avatar answered Oct 13 '22 20:10

Juuso Meriläinen