允许“ - “ Django Admin界面中的用户名字符 [英] Allowing the " - " character in usernames in the Django Admin interface

查看:206
本文介绍了允许“ - “ Django Admin界面中的用户名字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的webapp中,我们需要在我们的用户名中允许破折号 - 。
我已经启用了这个消费者注册过程,只要这个正则表达式
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>用户中编辑用户名,以允许用户名中的 - 字符?目前,我无法使用破折号来编辑任何用户名,因为它将返回用户名上的验证错误。

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 if可能。
我相当新的编程,但这是什么我将使用子类化?

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管理网站中点击添加用户时出现的表单。我们在这里所做的只是重新定义用户名字段,以使用我们改进的连字符接受的正则表达式,并更改 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的子类,它是用户模型默认使用的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 Admin界面中的用户名字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆