最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】给Django系统中添加一个新的用户

Django crifan 3976浏览 0评论

现有一个别人写的Django的系统,想要创建一个新的用户

可以看到数据库user_user中现有用户:

现在要去搞清楚,如何添加新用户

是手动插入mysql语句?

还是用manager管理脚本去添加?

本身启动本地服务的命令是:

python3 ./manage.py runserver_plus 65000

所以想到去试试help,果然有命令介绍:

➜  xx git:(master) ✗ python3 ./manage.py help

Type ‘manage.py help <subcommand>’ for help on a specific subcommand.

Available subcommands:

[auth]

    changepassword

    createsuperuser

[contenttypes]

    remove_stale_contenttypes

[django]

    check

    compilemessages

    createcachetable

    dbshell

    diffsettings

    dumpdata

    flush

    inspectdb

    loaddata

    makemessages

    makemigrations

    migrate

    sendtestemail

    shell

    showmigrations

    sqlflush

    sqlmigrate

    sqlsequencereset

    squashmigrations

    startapp

    startproject

    test

    testserver

[django_extensions]

    admin_generator

    clean_pyc

    clear_cache

    compile_pyc

    create_app

    create_command

    create_jobs

    create_template_tags

    delete_squashed_migrations

    describe_form

    drop_test_database

    dumpscript

    export_emails

    find_template

    generate_secret_key

    graph_models

    mail_debug

    notes

    passwd

    pipchecker

    print_settings

    print_user_for_session

    reset_db

    runjob

    runjobs

    runprofileserver

    runscript

    runserver_plus

    set_default_site

    set_fake_emails

    set_fake_passwords

    shell_plus

    show_template_tags

    show_templatetags

    show_urls

    sqlcreate

    sqldiff

    sqldsn

    sync_s3

    syncdata

    unreferenced_files

    update_permissions

    validate_templates

[script]

    import_scripts

[sessions]

    clearsessions

[staticfiles]

    collectstatic

    findstatic

    runserver

[user]

    add_initial_function_group

好像没有 create user之类的命令

django 新增用户

想起来之前的:

【已解决】Django后台去重置用户的加了密的密码

是通过后台管理页面重置密码的

那是不是也有新建用户的功能?

去看看

http://localhost:65000/admin/login/?next=/admin/

果然有

去试试:

【已解决】Django添加用户出错:django.db.utils.IntegrityError 1062 Duplicate entry for key mobile_phone_number

然后:

【已解决】Django后台管理页面中给新增用户添加自定义字段

然后再去激活和添加设置用户权限组等内容:

http://localhost:65000/admin/user/user/

而具体的用户组等内容如何选择,去参考别人的设置

然后再去更新为:

但是登录进来后,看不到剧本编写列表:

找了下,原来是需要后台管理页面去添加进来的:

加进来作为组员即可。

然后就可以看到和使用剧本编写功能了:

【总结】

已有Django项目,之前已有user的定义:

apps/user/admin.py

from django.contrib import admin

from django.contrib.auth.admin import UserAdmin as AuthUserAdmin

from .models import User, FunctionGroup

# Register your models here.

class UserAdmin(AuthUserAdmin):

    list_display = (‘id’, ‘mobile_phone_number’, ’email’, ‘username’, ‘name’,

                    ‘is_superuser’, ‘is_active’)

    list_filter = (‘is_superuser’, ‘is_active’)

    search_fields = (‘mobile_phone_number’, ’email’, ‘username’, ‘name’)

    fieldsets = AuthUserAdmin.fieldsets + ((None, {

        ‘fields’: (‘mobile_phone_number’, ‘name’)

    }), )

class FunctionGroupAdmin(admin.ModelAdmin):

    list_display = (‘id’, ‘owner’, ‘name’, ‘description’, ‘created_at’,

                    ‘updated_at’)

    search_fields = (‘owner__username’, ‘name’, ‘description’)

admin.site.register(User, UserAdmin)

admin.site.register(FunctionGroup, FunctionGroupAdmin)

然后此处:

  • 添加add_fieldsets:

    • 对比fieldsets,表示属性的集合

    • 那么add_fieldsets就表示 在add(新增)时候的fieldsets属性集合

    • 所以加上自己要的属性,即可

  • 如果想要深度定制化每个属性的名字和限制(比如最大长度等)

    • 那么再去继承UserCreationForm,加上对应的条件

加了后,完整的代码是:

from django.contrib import admin

from django.contrib.auth.admin import UserAdmin as AuthUserAdmin

from .models import User, FunctionGroup

from django.contrib.auth.forms import UserCreationForm

from django import forms

from django.utils.translation import ugettext_lazy as _

# Register your models here.

class UserAdmin(AuthUserAdmin):

    list_display = (‘id’, ‘mobile_phone_number’, ’email’, ‘username’, ‘name’,

                    ‘is_superuser’, ‘is_active’)

    list_filter = (‘is_superuser’, ‘is_active’)

    search_fields = (‘mobile_phone_number’, ’email’, ‘username’, ‘name’)

    fieldsets = AuthUserAdmin.fieldsets + ((None, {

        ‘fields’: (‘mobile_phone_number’, ‘name’)

    }), )

    add_fieldsets = (

        (

            None,

            {

                ‘classes’: (‘wide’,),

                ‘fields’: (‘name’, ‘username’, ‘mobile_phone_number’, ’email’, ‘password1’, ‘password2’)

            }

            # here are fields you want to have in django-admin AddUser view

            # one of the most important place above

        ),

    )

class FunctionGroupAdmin(admin.ModelAdmin):

    list_display = (‘id’, ‘owner’, ‘name’, ‘description’, ‘created_at’,

                    ‘updated_at’)

    search_fields = (‘owner__username’, ‘name’, ‘description’)

class UserCreationFormExtended(UserCreationForm):

    def __init__(self, *args, **kwargs):

        super(UserCreationFormExtended, self).__init__(*args, **kwargs)

        self.fields[‘name’] = forms.CharField(label=_("昵称"), max_length=128)

        self.fields[‘mobile_phone_number’] = forms.CharField(label=_("手机号"), max_length=11)

        self.fields[’email’] = forms.EmailField(label=_("邮箱"), max_length=50)

UserAdmin.add_form = UserCreationFormExtended

admin.site.register(User, UserAdmin)

admin.site.register(FunctionGroup, FunctionGroupAdmin)

界面上的效果是:

需要用有新增用户权限的,超级管理员去登录:

http://localhost:65000/admin/login/?next=/admin/

后才能添加:

http://localhost:65000/admin/

-》

http://localhost:65000/admin/user/user/add/

转载请注明:在路上 » 【已解决】给Django系统中添加一个新的用户

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.189 seconds, using 22.16MB memory