展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

数据库、sqlitestudio

  • 查看config 包下的settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
  • 项目根路径下会自动生成db.sqlite3
  • 项目根路径下执行如下命令,生成数据库
C:\work\bysms>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK
  • 使用chocolatey安装数据库sqlitestudio
choco install sqlitestudio

  • 连接:Database -> Add a database

  • 自定义数据库表
# 新建1个包
python manage.py startapp common 

# 在common包的models.py中编写如下
from django.db import models

class Customer(models.Model):
    # 客户名称
    name = models.CharField(max_length=200)
    # 联系电话
    phonenumber = models.CharField(max_length=200)
    # 地址
    address = models.CharField(max_length=200)
  • 更多类型参考

  • 在config包下的settings.py中编写如下

# 表示让django扫描common包
INSTALLED_APPS = [
    'common.apps.CommonConfig',
]

# 写法2
INSTALLED_APPS = [
    # 加入下面这行
    'common',
]
  • 项目根路径下执行如下命令
# 上面指定了django扫描common包,而common包又进行了修改,执行如下命令,会生成更行脚本
C:\work\bysms>python manage.py makemigrations common
Migrations for 'common':
  common\migrations\0001_initial.py
    - Create model Customer

  • 提交到数据库
C:\work\bysms>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, common, contenttypes, sessions
Running migrations:
  Applying common.0001_initial... OK
  • 查看数据库

  • 添加1个用户

# cmd进入项目根路径
C:\work\bysms>python manage.py createsuperuser
Username (leave blank to use 'ychen'): ychen
Email address: chniny@outlook.com
Password:                                                        # 密码至少8个字符
Password (again):
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

  • 添加用户方式2
# 浏览器输入
http://127.0.0.1/admin/
  • 输入上面的用户名和密码

posted @ 2022-10-27 13:51  DogLeftover  阅读(42)  评论(0编辑  收藏  举报