Using guardian
in a django
project, I want the admins to be able to assign object permissions over the admin interface. With guardian
this is possible, but the Object permissions form in the admin interface, the Group field is a TextField
. How do I make it a ChoiceField
with all existing groups as choices?
Is there a solution that only requires adding code in the admin.py
file of the app or do we have to overwrite some guardian
code? How can we do it without messing with the functionality of guardian
?
This is my admin.py
file:
from django.contrib import admin
from .models import MyModel
from guardian.admin import GuardedModelAdmin
class MyModelAdmin(GuardedModelAdmin):
pass
admin.site.register(MyModel, MyModelAdmin)
Here is the solution that works like a charm. First subclass the GuardedModelAdminMixin
Then define the ModelManager for groups with overriding the clean_group function:
Finally, use the new Mixin in your Admin class:
To do custom querysets on the user level, use this function in the new mixin:
A full example is here: LINK