refactor(src/projects): cache role names in role_display_name
Prevent repeated DB hits by caching Role objects and names.
This commit is contained in:
1 file changed
+6
-5
@@ -116,9 +116,10 @@ class BoardRolePermission(models.Model):
|
||||
|
||||
@property
|
||||
def role_display_name(self):
|
||||
from src.accounts.models import Role
|
||||
if not hasattr(BoardRolePermission, "_role_names"):
|
||||
from src.accounts.models import Role
|
||||
|
||||
try:
|
||||
return Role.objects.get(slug=self.role).name
|
||||
except Role.DoesNotExist:
|
||||
return self.role
|
||||
BoardRolePermission._role_names = {
|
||||
r.slug: r.name for r in Role.objects.only("slug", "name")
|
||||
}
|
||||
return BoardRolePermission._role_names.get(self.role, self.role)
|
||||
Reference in new issue
Block a user