1. 程式人生 > 其它 >django-模型層

django-模型層

django模型層




settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysite2',
        'USER':'root',
        'PASSWORD':'password',
        'HOST':'localhost', #'HOST':'127.0.0.1'
        'PORT':'3306'
    }
}

orm框架




settings.py

# E:\django_video_study\mysite2>python manage.py startapp bookstore

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

bookstore.models.py

from django.db import models

# Create your models here.
class  Book(models.Model):
    titel=models.CharField('書名',max_length=50,default='')
    price=models.DecimalField('價格',max_digits=7,decimal_places=2)

執行生成遷移檔案

E:\django_video_study\mysite2>python manage.py makemigrations
Migrations for 'bookstore':
  bookstore\migrations\0001_initial.py
    - Create model Book

E:\django_video_study\mysite2\bookstore>tree
卷 NewDisk 的資料夾 PATH 列表
卷序列號為 F077-0FBF
E:.
├─migrations
│  └─__pycache__
└─__pycache__

遷移檔案 bookstore/migrations/0001_initial.py

# Generated by Django 2.2.23 on 2021-08-06 01:35

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Book',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('titel', models.CharField(default='', max_length=50, verbose_name='書名')),
                ('price', models.DecimalField(decimal_places=2, max_digits=7, verbose_name='價格')),
            ],
        ),
    ]


建立遷移

E:\django_video_study\mysite2>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, bookstore, 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 bookstore.0001_initial... OK
  Applying sessions.0001_initial... OK

遷移成功顯示mysql資料

mysql> use mysite2
Database changed
mysql> show tables;
+----------------------------+
| Tables_in_mysite2          |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| bookstore_book             |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
11 rows in set (0.00 sec)

mysql> desc bookstore_book;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int          | NO   | PRI | NULL    | auto_increment |
| titel | varchar(50)  | NO   |     | NULL    |                |
| price | decimal(7,2) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)