Opened 6 months ago

Closed 6 months ago

#35563 closed Cleanup/optimization (wontfix)

Fetch related user permissions if "user_permissions" are added in add_fieldsets on Admin Panel

Reported by: Ali Hassan Owned by: alihassanc5
Component: contrib.auth Version: 5.0
Severity: Normal Keywords:
Cc: Ali Hassan Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

So I created my custom UserAdmin which is inherited from Base UserAdmin class, I am using default Django model.
Python: 3.10.14
DB Engine: Postgres

I have added user_permissions in add_fieldsets:

add_fieldsets = (
        (
            None,
            {
                "classes": ("wide",),
                "fields": (
                    "first_name",
                    "last_name",
                    "email",
                    "password1",
                    "password2",
                    "is_active",
                    "is_staff",
                    "is_superuser",
                    "user_permissions",
                ),
            },
        ),
    )


user_permissions causing an issue because it's fetching all the related content-type in a separate query.

So, my proposed solutions is to fetch all the related permissions if it's in fieldset, same like we are doing in our UserChangeForm

class UserChangeForm(forms.ModelForm):
    password = ReadOnlyPasswordHashField(
        label=_("Password"),
        help_text=_(
            "Raw passwords are not stored, so there is no way to see this "
            "user’s password, but you can change the password using "
            '<a href="{}">this form</a>.'
        ),
    )

    class Meta:
        model = User
        fields = "__all__"
        field_classes = {"username": UsernameField}

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        password = self.fields.get("password")
        if password:
            password.help_text = password.help_text.format(
                f"../../{self.instance.pk}/password/"
            )
        user_permissions = self.fields.get("user_permissions")
        if user_permissions:
            user_permissions.queryset = user_permissions.queryset.select_related(
                "content_type"
            )

Could you please assign this issue to me? I will be happy to provide the solution, thanks!

Attachments (1)

image-20240626-155934.png (35.1 KB ) - added by Ali Hassan 6 months ago.

Download all attachments as: .zip

Change History (3)

by Ali Hassan, 6 months ago

Attachment: image-20240626-155934.png added

comment:1 by Ali Hassan, 6 months ago

Component: contrib.admincontrib.auth

comment:2 by Natalia Bidart, 6 months ago

Resolution: wontfix
Status: assignedclosed

Hello Ali Hassan, thank you for your interest in making Django better!

I'm a little undecided about how to triage this issue, on one hand I can see value in what you are proposing, but on the other hand we need to be mindful of existing projects using/overriding UserAdmin and how a change like this may disrupt them.

As far as I understand your description, you could definitely define your own prefetch for the user_permissions in your custom UserAdmin. If that is indeed the case, it seems a very specific need arising from a niche use case, in which case I don't think this applies to the broader ecosystem.

Given the above, I'll close the ticket accordingly, but if you disagree, you can consider starting a new conversation on the Django Forum, where you'll reach a wider audience and likely get extra feedback. More information in the documented guidelines for requesting features.

Note: See TracTickets for help on using tickets.
Back to Top