[Django] #35563: Fetch related user permissions if "user_permissions" are added in add_fieldsets on Admin Panel

5 views
Skip to first unread message

Django

unread,
Jun 26, 2024, 6:58:51 AM (3 days ago) Jun 26
to django-...@googlegroups.com
#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.

Django

unread,
Jun 26, 2024, 6:59:41 AM (3 days ago) Jun 26
to django-...@googlegroups.com
#35563: Fetch related user permissions if "user_permissions" are added in
add_fieldsets on Admin Panel
-------------------------------------+-------------------------------------
Reporter: Alihassanc5 | Owner:
Type: | alihassanc5
Cleanup/optimization | Status: assigned
Component: contrib.admin | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Alihassanc5):

* Attachment "image-20240626-155934.png" added.

Django

unread,
Jun 26, 2024, 7:08:11 AM (3 days ago) Jun 26
to django-...@googlegroups.com
#35563: Fetch related user permissions if "user_permissions" are added in
add_fieldsets on Admin Panel
-------------------------------------+-------------------------------------
Reporter: Alihassanc5 | Owner:
Type: | alihassanc5
Cleanup/optimization | Status: assigned
Component: contrib.auth | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Alihassanc5):

* component: contrib.admin => contrib.auth

--
Ticket URL: <https://code.djangoproject.com/ticket/35563#comment:1>

Django

unread,
Jun 26, 2024, 5:07:59 PM (3 days ago) Jun 26
to django-...@googlegroups.com
#35563: Fetch related user permissions if "user_permissions" are added in
add_fieldsets on Admin Panel
-------------------------------------+-------------------------------------
Reporter: Ali Hassan | Owner:
Type: | alihassanc5
Cleanup/optimization | Status: closed
Component: contrib.auth | Version: 5.0
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* resolution: => wontfix
* status: assigned => closed

Comment:

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
[https://forum.djangoproject.com/c/internals/5 Django Forum], where you'll
reach a wider audience and likely get extra feedback. More information in
[https://docs.djangoproject.com/en/stable/internals/contributing/bugs-and-
features/#requesting-features the documented guidelines for requesting
features].
--
Ticket URL: <https://code.djangoproject.com/ticket/35563#comment:2>
Reply all
Reply to author
Forward
0 new messages