It appears that formfield_for_manytomany() triggers an error if it is used in UserAdmin.

Thanks for any support in deciding if this is a bug - or perhaps all my own work.

Cheers

Mike

TL;DR

Here is all the admin code which works without error if formfield_for_manytomany()is commented out ...

from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from django.db import models
from common.models import UserProfile
from common.utils import (
    is_sharedsds, user_sharedsds_admin, userprofile_sharedsds_admin,
)
from common.widgets import CommonFilteredSelectMultiple

class CommonUserAdmin(UserAdmin):

    def formfield_for_manytomany(self, db_field, request, **kwargs):
        pass

        #if db_field.name == "groups":
        #    formfield_overrides = {
        #        models.ManyToManyField: {'widget': CommonFilteredSelectMultiple},
        #    }

    model = get_user_model()
    extra = 0
    readonly_fields = [
        "last_login",
        "is_staff",
        "is_active",
        "date_joined",
        "groups",
    ]
    # user_sharedsds_admin() makes some of the above r/o fields r/w only for sharedsds members
    get_readonly_fields = user_sharedsds_admin
    fieldsets = [
        (
            "More detail",
            {
                "description": "Server date/times are UTC rather than local time<hr/>",
                "classes": ("collapse",),
                "fields": (
                    "password",
                    "last_login",
                    "username",
                    "first_name",
                    "last_name",
                    "email",
                    "is_staff",
                    "is_active",
"groups",
                    "date_joined",
                ),
            },
        ),
    ]

    class UserProfileInline(admin.StackedInline):
        def has_add_permission(self, request=None, obj=None):
            return False

        def has_delete_permission(self, request=None, obj=None):
            return False

        model = UserProfile
        extra = 0
        verbose_name_plural = "Profile"
        readonly_fields = ("company", "modified",)
        # userprofile_sharedsds_admin() makes some of the above r/o fields r/w for sharedsds members
        get_readonly_fields = userprofile_sharedsds_admin
        fieldsets = (
            (
                "Cellphone, Company",
                {
                    "classes": ("collapse",),
                    "fields": ("cellphone", "company",),
                },
            ),
            (
                "Preferences",
                {
                    "classes": ("collapse",),
                    "fields": (
                        "code_preference",
                        "subsections",
                        "licensed_data",
                        "open_data",
                        "ingredient_data",
                        "modified",
                    ),
                },
            ),
        )
    inlines = [
        UserProfileInline,
    ]

admin.site.register(get_user_model(), CommonUserAdmin)


--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/171e6dcb-878a-551a-6ce3-2b453bc46e03%40dewhirst.com.au.

Reply via email to