Thursday, September 3, 2020

Admin - django.auth.group 1to1 comment

In the Admin I would like to add an inline comment to django.auth.group
for purposes of documenting the role each group record represents
including why certain permissions have been granted or denied to that
group. In other words, I believe auth.group really needs a comment
TextField.

I've struck a wall and would appreciate any hints. This is what I've done:

1. Created a comment table like this:

from django.db import models


class Comment(models.Model):
    """ Documentation for sets of group permissions. Just in case we
forget. """

    group = models.OneToOneField(
        "auth.Group",
        on_delete=models.CASCADE,
        related_name='groupcomment'
    )
    comment = models.TextField(
        verbose_name='comment',
        default='',
        blank=True,
        help_text='Document this group',
    )
    class Meta:
        verbose_name = 'comment'
        verbose_name_plural = 'comments'

    def __str__(self):
        return '{0} comment'.format(self.group)


2. Made a class in admin.py (in my "common" app which houses my custom
user) like this:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin, Group, GroupAdmin
from django.contrib.admin.options import InlineModelAdmin
from common.models import Comment, User, UserProfile

class MyGroupAdmin(GroupAdmin):

    model = Group

    class CommentInline(admin.StackedInline):
        model = Comment
        extra = 0
        fieldsets = [
            (
                "Comment",
                {
                    "description": "Server date/times are UTC rather
than local time<hr/>",
                    "classes": ("",),
                    "fields": (
                        "comment",
                    ),
                },
            ),
        ]
    inlines = ((CommentInline,),)

# omitting UserAdmin etc

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


3. So the error turns out to be:

ERRORS:
<class 'common.admin.MyGroupAdmin'>: (admin.E104) 'auth.MyGroupAdmin'
must inherit from 'InlineModelAdmin'.


4. When I import and include 'InlineModelAdmin' like this:

class MyGroupAdmin(GroupAdmin, InlineModelAdmin):
     ...

The error becomes:

<snip>
  File "D:\Users\mike\envs\xxct3\train\common\admin.py", line 116, in
<module>
    admin.site.register(Group, MyGroupAdmin)
  File
"D:\Users\mike\envs\xxct3\lib\site-packages\django\contrib\admin\sites.py",
line 124, in register
    self._registry[model] = admin_class(model, self)
  File
"D:\Users\mike\envs\xxct3\lib\site-packages\django\contrib\admin\options.py",
line 580, in __init__
    super().__init__()
TypeError: __init__() missing 2 required positional arguments:
'parent_model' and 'admin_site'


I have tried using super().__init__() to resolve these errors but the
trail just goes deeper into the forest and I'm scrabbling in the dark at
this stage.

Can anyone please help?

Thanks

Mike



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f2704ba9-4127-7462-26d2-c143f2b3459b%40dewhirst.com.au.

No comments:

Post a Comment