#35563: Fetch related user permissions if "user_permissions" are added in
add_fieldsets on Admin Panel
-------------------------------------+-------------------------------------
Reporter: | Owner: alihassanc5
Alihassanc5 |
Type: | Status: assigned
Cleanup/optimization |
Component: | Version: 5.0
contrib.admin |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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!
--
Ticket URL: <
https://code.djangoproject.com/ticket/35563>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.