Django超级用户创建、编辑及管理用model

前言

  • django框架超级用户主要是用来解决应用开发者或所有人管理应用后台数据的。应用model的配置直接关系到超级用户操作对应数据的方便程度。
  • 同时,我们可能会忘记已经创建的超级用户的名称或者密码,所以势必要面临着如何找回/重置密码,修改密码甚至修改用户名的情况。阅读本文可以基本能够解决以上问题。

创建

1.新建一个用户名,使用如下命令:

python manage.py createsuperuser

2.输入打算使用的登录名:

Username(leave blank to use 'administrator'):wangchenxi

3.输入email:

Email address:

4.输入密码,需要输入两次,输入过程中密码不显示:

Password:

Password(again):

5.当两次密码都相同的时候,就会提示superuser创建成功。

Superuser created successfully

6.接下来运行服务:

python manage.py runserver

在浏览器打开:127.0.0.1:8000/admin

编辑修改

直接阅读代码如下:

#! /usr/bin/python3
# -*- coding:utf-8 -*-
# file: terminal_django_models.py
# author: wangchenxi
# mail: wongchenxi@icloud.com
# brief:
# version: 0.1.00
# Create Time:2019-12-03 16:06:33
# Last Update: 2019-12-03 16时10分02秒

project_name = input('输入项目名称:')
settings = '%s.settings' %project_name

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings)
import django
django.setup()
from crm import models
from django.contrib.auth.models import User
# 查询用户名
user = User.objects.get(pk=1)
# user = User.objects.get(username='your account')
# 修改超级用户密码
user.set_password('11223344')
# 保存超级用户密码
user.save()
# 退出
quit()

配置管理应用model

在应用admin.py中根据models.py注册设置

例如应用models.py如下:

from django.db import models


class BlogArticle(models.Model):
    title = models.CharField(
        max_length=1024, unique=True,
        verbose_name='标题')
    create_datetime = models.DateTimeField(
        auto_now_add=True, verbose_name='创建时间')
    last_update_datetime = models.DateTimeField(
        auto_now=True, verbose_name='最后更新时间')
    content = models.TextField(
        verbose_name="文章内容", max_length=65535)
    img_url = models.ForeignKey(
        'Image', default=1, blank=True, verbose_name="图片",
        on_delete=models.SET_DEFAULT)
    category = models.ManyToManyField(
        'ArticleCategory', verbose_name="类别")
    label = models.ManyToManyField(
        'ArticleLabel', verbose_name='标签')
    weight = models.IntegerField(default='0', blank=True, null=False)

    def __str__(self):
        return "<BlogArticle obj:{} {}>".format(self.id, self.title)

与上面对应的admin.py如下配置:

from django.contrib import admin
from rbac import models
from django.conf import global_settings


class BlogArticleConfig(admin.ModelAdmin):
    list_display = [
        'id', 'title', 'create_datetime', 'last_update_datetime',
        'img_url', 'weight']
    list_editable = [
        'title', 'img_url', 'weight']
    search_fields = ('title', 'category__name', 'label__name')  # 添加搜索字段
    filter_horizontal = ('category', 'label')  # 只针对多对多
    list_filter = ('category', 'label')
    list_per_page = 10  # 让每页显示几条记录的设置
    # raw_id_fields = ('publisher',)    # 只针对外键的


admin.site.register(models.BlogArticle, BlogArticleConfig)

版权声明:除特别注明外,本站所有文章均为王晨曦个人站点原创

转载请注明:出处来自王晨曦个人站点 » Django超级用户创建、编辑及管理用model

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注

  1. Viagra 说道:

    Viagra Per Ansia Da Prestazione

  2. Stromectol 说道:

    Acheter Lioresal 25mg

  3. Adjucky 说道:

    https://buysildenshop.com/ - venta de viagra

  4. doxosse 说道:

    https://buypropeciaon.com/ - what happens if a woman takes propecia

  5. visathy 说道:

    http://buytadalafshop.com/ - best place to buy cialis online

  6. Propecia 说道:

    Best Price For Otc Nexium

  7. antimaNib 说道:

    https://buystromectolon.com/ - buy ivermectin pills

  8. I simply wished to appreciate you all over again. I am not sure the things that I could possibly have tried without the entire tactics contributed by you about such a question. It became the depressing concern for me personally, nevertheless being able to see this specialized mode you resolved the issue took me to cry for gladness. Extremely happier for your support and in addition sincerely hope you are aware of an amazing job that you are providing educating the rest with the aid of your website. I know that you've never encountered all of us.

  9. vsolewshk 说道:

    出发地 vsolewshk http://www.gj2n217wraoj752sxm7wfg6378q22j92s.org/ [url=http://www.gj2n217wraoj752sxm7wfg6378q22j92s.org/]uvsolewshk[/url] avsolewshk

  10. Wheesty 说道:

    https://buyzithromaxinf.com/ - Zithromax

  11. Dalpipt 说道:

    https://buylasixshop.com/ - Lasix