zoukankan      html  css  js  c++  java
  • 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_studymysite2>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_studymysite2>python manage.py makemigrations
    Migrations for 'bookstore':
      bookstoremigrations001_initial.py
        - Create model Book
    
    E:django_video_studymysite2ookstore>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_studymysite2>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)
    
    
    

    image
    模型类名 对应数据库表名中app名+模型小写开头类名

  • 相关阅读:
    浅谈localStorage和sessionStorage的相关用法
    v-for中:key的作用总结
    textarea的placeholder无效问题解决
    6月10日
    6月9日
    6月8日
    6月7日
    6月6日
    10月5日
    6月4日
  • 原文地址:https://www.cnblogs.com/yescarf/p/15107171.html
Copyright © 2011-2022 走看看