summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormariatta <mariatta.wijaya@gmail.com>2026-04-03 11:44:02 -0700
committerJacob Walls <jacobtylerwalls@gmail.com>2026-04-07 15:38:35 -0400
commite2abe321a6f1370e05c1a89a742125c9eafcac8c (patch)
tree85bd11175bfa5e3e3cc9e7ccbb9c3af31cc37f4e
parent74e73dc1315d696330621a7f08310a2e87ea0eba (diff)
Fixed #37021 -- Added Permission.user_perm_str property.
For use in checking user permissions via has_perm(). Co-authored-by: 사재혁 <jaehyuck.sa.dev@gmail.com>
-rw-r--r--django/contrib/auth/models.py5
-rw-r--r--docs/ref/contrib/auth.txt10
-rw-r--r--docs/releases/6.1.txt3
-rw-r--r--docs/topics/auth/default.txt9
-rw-r--r--tests/auth_tests/test_models.py4
5 files changed, 31 insertions, 0 deletions
diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
index 7b7dee78fc..f41ece7ce8 100644
--- a/django/contrib/auth/models.py
+++ b/django/contrib/auth/models.py
@@ -79,6 +79,11 @@ class Permission(models.Model):
def __str__(self):
return "%s | %s" % (self.content_type, self.name)
+ @property
+ def user_perm_str(self):
+ """String representation for the user permission check."""
+ return f"{self.content_type.app_label}.{self.codename}"
+
def natural_key(self):
return (self.codename, *self.content_type.natural_key())
diff --git a/docs/ref/contrib/auth.txt b/docs/ref/contrib/auth.txt
index ff16de6423..b09b55cdda 100644
--- a/docs/ref/contrib/auth.txt
+++ b/docs/ref/contrib/auth.txt
@@ -405,6 +405,16 @@ Methods
:class:`~django.contrib.auth.models.Permission` objects have the standard
data-access methods like any other :doc:`Django model </ref/models/instances>`.
+.. class:: models.Permission
+ :noindex:
+
+ .. attribute:: user_perm_str
+
+ .. versionadded:: 6.1
+
+ Returns the string representation for use in :meth:`has_perm
+ <django.contrib.auth.models.User.has_perm>`.
+
``Group`` model
===============
diff --git a/docs/releases/6.1.txt b/docs/releases/6.1.txt
index 9c8aeea70c..00eaf5388d 100644
--- a/docs/releases/6.1.txt
+++ b/docs/releases/6.1.txt
@@ -129,6 +129,9 @@ Minor features
* :attr:`.Permission.name` and :attr:`.Permission.codename` values are now
renamed when renaming models via a migration.
+* The new :attr:`.Permission.user_perm_str` property returns the string
+ suitable to use with :meth:`.User.has_perm`.
+
:mod:`django.contrib.contenttypes`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/docs/topics/auth/default.txt b/docs/topics/auth/default.txt
index 77888febb3..83925d009f 100644
--- a/docs/topics/auth/default.txt
+++ b/docs/topics/auth/default.txt
@@ -243,6 +243,15 @@ to test for basic permissions you should use:
The :class:`~django.contrib.auth.models.Permission` model is rarely accessed
directly.
+The :class:`~django.contrib.auth.models.Permission` model provides a helper
+property, :attr:`~django.contrib.auth.models.Permission.user_perm_str`,
+that returns the string representation that can be used to check permission
+using the :meth:`~django.contrib.auth.models.User.has_perm` method.
+
+.. versionchanged:: 6.1
+
+ The ``user_perm_str`` property was added.
+
Groups
------
diff --git a/tests/auth_tests/test_models.py b/tests/auth_tests/test_models.py
index a3e7a3205b..fd99b970d7 100644
--- a/tests/auth_tests/test_models.py
+++ b/tests/auth_tests/test_models.py
@@ -635,3 +635,7 @@ class PermissionTests(TestCase):
self.assertEqual(
str(p), "Auth_Tests | custom email field | Can view custom email field"
)
+
+ def test_user_perm_str(self):
+ p = Permission.objects.get(codename="view_customemailfield")
+ self.assertEqual(p.user_perm_str, "auth_tests.view_customemailfield")