I want to build a custom view that gives ( or take depending on the situation) permission(s) to user. What I want to do is to produce a form with all the permissions listed with check boxes next to each permission, checked already against a permission given to user. In simpler words, I want to make the customised version of Django Admin where I can give or take back the permissions. How can I do this?
I can get a list of permissions by using
from django.contrib.auth.models import Permission
per=Permission.objects.all()
similarly I can get the user object by using user=User.objects.get(id=id)
model. But how can I produce a form with which has check boxes and a view to connect all the checked/unchecked permissions to the user?
This is my views.py
class UserUpdate(UserPassesTestMixin, UpdateView):
models = User
fields = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'is_active', 'is_superuser',
'user_permissions')
def get_object(self, queryset=None):
obj = User.objects.get(pk=self.kwargs['pk'])
self.success_url = reverse_lazy('admin:user_detail', kwargs={'pk': obj.pk})
return obj
def test_func(self):
return self.request.user.is_superuser
{% extends 'admin_app/base.html' %}
{% load bootstrap4 %}
{% block content_class %}
<form method="POST">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" value="Update">
</form>
{% endblock %}
Template
I want the page to show all the given permissions to user+ add/remove them just like it is in the Admin Panel.