Python圈Python基础

Django基础3--数据库配置及创建模型

2020-08-26  本文已影响0人  伊洛的小屋
1.数据库配置
2.注册APP
# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

'django.contrib.admin', 管理员站点
'django.contrib.auth', 认证系统
'django.contrib.contenttypes', 内容类型框架
'django.contrib.sessions', 会话框架
'django.contrib.messages', 消息框架
'django.contrib.staticfiles', 管理静态文件框架

3.数据库迁移
# 伊洛Yiluo 公众号:伊洛的小屋
# https://yiluotalk.com/
python manage.py migrate
(django) ➜  AutoPlatform 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
4.创建模型
# 作者:伊洛Yiluo 公众号:伊洛的小屋
# 个人主页:https://yiluotalk.com/
# 博客园:https://www.cnblogs.com/yiluotalk/
from django.db import models

# Create your models here.


class ProjectInfo(models.Model):
    """ 项目信息模型 """
    project_name = models.CharField(max_length=30)
    dev = models.CharField(max_length=20)
    tester = models.CharField(max_length=20)
    add_data = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        # 返回项目名称
        return self.project_name

5.激活模型
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # My app
    'autoapi',
]
(django) ➜  AutoPlatformpython manage.py makemigrations autoapi
Migrations for 'autoapi':
  autoapi/migrations/0001_initial.py
    - Create model ProjectInfo

(django) ➜  AutoPlatform python manage.py migrate 
Operations to perform:
  Apply all migrations: admin, auth, autoapi, contenttypes, sessions
Running migrations:
  Applying autoapi.0001_initial... OK
6.总结

模型需要这三步

欢迎下方【戳一下】【点赞】
Author:伊洛Yiluo
愿你享受每一天,Just Enjoy !

上一篇下一篇

猜你喜欢

热点阅读