﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35563	"Fetch related user permissions if ""user_permissions"" are added in add_fieldsets on Admin Panel"	Ali Hassan	alihassanc5	"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!"	Cleanup/optimization	closed	contrib.auth	5.0	Normal	wontfix		Ali Hassan	Unreviewed	0	0	0	0	0	0
