蓝鲸Django: 创建数据库

来自CloudWiki
跳转至: 导航搜索

创建模型(models)

创建两个模型,WeChatUser和Status

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class WeChatUser(models.Model):
    user = models.OneToOneField(User, models.CASCADE)
    motto = models.CharField(max_length=200,null=True,blank=True)
    pic = models.CharField(max_length=50,null=True,blank=True)

    def __str__(self):
        return self.user.username

class Status(models.Model):
    user = models.ForeignKey(WeChatUser, models.CASCADE)
    text = models.CharField(max_length=500)
    pics = models.CharField(max_length=200, null=True, blank=True)
    pub_time = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.text

    class Meta:
        ordering = ["id"]

模型迁移及写入

此步的作用是将模型写入数据库。这里貌似是跟使用的数据库无关的 ?

[root@localhost wechat]# python3 manage.py makemigrations

Migrations for 'moments':
  moments/migrations/0001_initial.py
    - Create model Status
    - Create model WeChatUser
    - Add field user to status

[root@localhost wechat]# python3 manage.py sqlmigrate moments 0001

BEGIN;
--
-- Create model Status
--
CREATE TABLE "moments_status" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "text" varchar(500) NOT NULL, "pics" varchar(200) NULL, "pub_time" datetime NOT NULL);
--
-- Create model WeChatUser
--
CREATE TABLE "moments_wechatuser" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "motto" varchar(200) NULL, "pic" varchar(50) NULL, "user_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);
--
-- Add field user to status
--
ALTER TABLE "moments_status" RENAME TO "moments_status__old";
CREATE TABLE "moments_status" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "text" varchar(500) NOT NULL, "pics" varchar(200) NULL, "pub_time" datetime NOT NULL, "user_id" integer NOT NULL REFERENCES "moments_wechatuser" ("id") DEFERRABLE INITIALLY DEFERRED);
INSERT INTO "moments_status" ("id", "text", "pics", "pub_time", "user_id") SELECT "id", "text", "pics", "pub_time", NULL FROM "moments_status__old";
DROP TABLE "moments_status__old";
CREATE INDEX "moments_status_user_id_d38910b1" ON "moments_status" ("user_id");
COMMIT;

[root@localhost wechat]# python3 manage.py migrate

 Operations to perform:
  Apply all migrations: admin, auth, contenttypes, moments, 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 moments.0001_initial... OK
  Applying sessions.0001_initial... OK

admin管理

数据库查看,有没有一种可视化的方式?

用admin组件可以。

编写admin.py

moments.admin.py:

from django.contrib import admin
from .models import WeChatUser, Status
# Register your models here.


admin.site.register(WeChatUser)
admin.site.register(Status)
~

创建超级用户

此步为浏览器登陆admin做准备。

[root@localhost wechat]# python3 manage.py createsuperuser

Username (leave blank to use 'root'):
Email address: maxin5452@qq.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

Bd20-3-7.png

浏览器登陆admin

Bd20-3-8.png

Bd20-3-9.png