问题描述
在我们的 web 应用中,我们需要在用户名中允许使用破折号-".我已经为消费者注册过程启用了这个正则表达式r'^[w-]+$'
In our webapp we needed to allow dashes "-" in our usernames. I've enabled that for the consumer signup process just fine with this regex r'^[w-]+$'
如何告诉管理应用程序以便我可以在 auth > users 中编辑用户名以允许用户名中的-"字符?目前我无法编辑任何带有破折号的用户名,因为它会在用户名上返回验证错误.
How can I tell the admin app so that I can edit usernames in auth > users to allows the "-" character in usernames? Currently I am unable to edit any usernames with dashes in them as it will return a validation error on the username.
如果可能,我想尽量避免直接修补 django.我对编程还很陌生,但这就是我要使用子类化"的目的吗?
I'd like to try and avoid patching django directly if possible. I'm fairly new to programming but is this what I would use "subclassing" for?
推荐答案
这应该像覆盖 User ModelAdmin 类的行为一样简单.在您的一个应用程序中,在 admin.py
中包含以下代码.
This should be as simple as overriding the behavior of the User ModelAdmin class. In one of your apps, in admin.py
include the following code.
from django.contrib import admin
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
class MyUserCreationForm(UserCreationForm):
username = forms.RegexField(
label='Username',
max_length=30,
regex=r'^[w-]+$',
help_text = 'Required. 30 characters or fewer. Alphanumeric characters only (letters, digits, hyphens and underscores).',
error_message = 'This value must contain only letters, numbers, hyphens and underscores.')
class MyUserChangeForm(UserChangeForm):
username = forms.RegexField(
label='Username',
max_length=30,
regex=r'^[w-]+$',
help_text = 'Required. 30 characters or fewer. Alphanumeric characters only (letters, digits, hyphens and underscores).',
error_message = 'This value must contain only letters, numbers, hyphens and underscores.')
class MyUserAdmin(UserAdmin):
form = MyUserChangeForm
add_form = MyUserCreationForm
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
这里有一点解释.
第一个类定义 (MyUserCreationForm) 是 UserCreationForm 的子类(是的,您的术语是正确的).这是在 Django 管理站点中单击添加用户"时出现的表单.我们在这里所做的只是重新定义 username
字段以使用我们改进的、接受连字符的正则表达式,并更改 helptext
以反映这一点.
The first class definition (MyUserCreationForm) is a subclass (yes your terminology is correct) of the UserCreationForm. This is the form that appears when you click "Add User" in the Django Admin site. All we are doing here is redefining the username
field to use our improved, hyphen-accepting regex, and changing the helptext
to reflect this.
第二个类的定义是一样的,除了 UserChangeForm.
The second class definition does the same, except for the UserChangeForm.
最终的类定义是UserAdmin的子类,也就是User模型默认使用的ModelAdmin.在这里,我们声明我们希望在 ModelAdmin 中使用我们新的自定义表单.
The final class definition is a subclass of UserAdmin, which is the ModelAdmin that the User model uses by default. Here we state that we want to use our new custom forms in the ModelAdmin.
请注意,对于这些子类中的每一个,我们只更改我们必须更改的内容.该类的其余部分将继承自其父级(分别为 UserCreationForm、UserChangeForm 和 UserAdmin).
Note that for each of these subclasses, we only change what we have to. The rest of the class will be inherited from its parent (UserCreationForm, UserChangeForm and UserAdmin respectively).
最后,我们执行向管理站点注册用户模型的重要步骤.为此,我们取消注册默认的 UserAdmin,然后使用我们改进的 MyUserAdmin 类进行注册.
Finally, we perform the important step of registering the User model with the admin site. To do that we unregister the default UserAdmin, and then register with our improved MyUserAdmin class.
您会发现使用这些技术可以很容易地自定义 Django 管理站点,尤其是考虑到管理站点只是一个普通的 Django 应用程序.
You'll find that the Django admin site is very easy to customize using these techniques, especially considering the admin site is just a regular Django app.
这篇关于允许"-"Django 管理界面中用户名中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!