如何修改Admin Panel中标准权限文本及替换指定权限文本为<select>
Got it, let's break down how to tackle both of these admin panel modifications. I'll assume you're working with a Python/Django admin setup (one of the most common), but the core concepts can be adapted to other frameworks too.
Standard permission labels (like "Can add Modelname" or "Can edit Modelname") are usually tied to your model definitions or framework-specific permission systems. Here's how to update them:
Override permissions directly in your model:
If you want custom labels for default model permissions, define them explicitly in your model'sMetaclass. This replaces the auto-generated labels with your own:from django.db import models class Product(models.Model): name = models.CharField(max_length=100) price = models.DecimalField(max_digits=10, decimal_places=2) class Meta: # Replace default permission labels with custom text permissions = [ ("add_product", "Create new Product listings"), ("change_product", "Modify existing Product details"), ("delete_product", "Remove Product entries"), ("view_product", "View Product information"), ]Use translation files (for multilingual setups):
If you need to keep permissions flexible across languages, update your framework's translation files. For Django:- Run
python manage.py makemessagesto generate translation files. - Open the
.pofile for your language (e.g.,locale/en/LC_MESSAGES/django.po). - Search for the default permission strings and replace them with your custom text:
msgid "Can add %s" msgstr "Create new %s entries"
- Run
Custom admin panels:
If you're using a fully custom admin system, look for permission definition files (often constants or language JSON files) and update the text values directly.
<select> Element For permissions formatted as inc|Modelname|Can add permissions on: Modelname|add, we need to modify how these are rendered in the admin template and handle form submission to update the value.
Step 1: Locate the permission rendering template
Find the template file where permissions are displayed (e.g., admin/auth/user/change_form.html in Django, or a custom permissions.html partial). This is where we'll replace the text with a dropdown.
Step 2: Add logic to render the <select>
Use template syntax to detect the permission pattern, extract the model name, and replace the text with a dropdown. Example for Django:
{% for permission in user.user_permissions.all %} {% if permission.name|startswith:"inc|" %} {% with perm_parts=permission.name|split:"|" %} {% if perm_parts.3 == "add" %} <div class="permission-row"> <label>Can add permissions on:</label> <select name="perm_model_{{ permission.id }}"> {% for model in all_registered_models %} <option value="{{ model.model_name }}" {% if model.model_name == perm_parts.1 %}selected{% endif %} > {{ model.verbose_name }} </option> {% endfor %} </select> <input type="checkbox" name="selected_perms" value="{{ permission.id }}" checked> </div> {% endif %} {% endwith %} {% else %} <!-- Render standard permission labels normally --> <div class="permission-row"> <label>{{ permission.name }}</label> <input type="checkbox" name="selected_perms" value="{{ permission.id }}" checked> </div> {% endif %} {% endfor %}
Note: You'll need to pass all_registered_models (a list of your app's models with their model_name and verbose_name) to the template from your view.
Step 3: Handle form submission to update the permission
In your view that processes the permission form, capture the selected model from the dropdown and update the permission's name to match the required pattern:
from django.contrib.auth.models import Permission def update_user_permissions(request, user_id): if request.method == "POST": selected_perm_ids = request.POST.getlist("selected_perms") for perm_id in selected_perm_ids: perm = Permission.objects.get(id=perm_id) # Get the selected model from the dropdown selected_model = request.POST.get(f"perm_model_{perm_id}") if selected_model: # Update the permission name to match the pattern perm.name = f"inc|{selected_model}|Can add permissions on: {selected_model}|add" perm.save() # Redirect back to the user's permission page return redirect("admin:auth_user_change", user_id)
Step 4: Add validation (optional)
To ensure only valid models are selected, add a check in your view to verify that selected_model matches one of your registered models:
from django.apps import apps valid_models = [model.__name__ for model in apps.get_models()] if selected_model not in valid_models: # Handle invalid input (e.g., show error message) continue
内容的提问来源于stack exchange,提问作者Aleks Porberevskiy

