django 1.9版本数据库建表

django 1.9版本建数据表过程:

环境:windows 、pycharm版本4.0、django版本1.9

1. 新建django项目,建一个app

file-new project --django --写项目名称--more setting ---写app名称--create

2.设置setting.py

数据库设置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': 'password',                  # Not used with sqlite3.
        'HOST': '10.xxx.xxx.xxx',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '3306',                      # Set to empty string for default. Not used with sqlite3.
    }
}

添加app

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'books',
)

3.app中的model.py  设置

from django.db import models

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

class Author(models.Model):
    salutation = models.CharField(max_length=10)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()
    headshot = models.ImageField(upload_to='/tmp')

class Book  (models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

4.在项目目录下运行命令

新建sql文件:

python manage.py makemigrations

$ python manage.py makemigrations
Migrations for 'books':
  0001_initial.py:
    - Create model Author
    - Create model Book
    - Create model Publisher
    - Add field publisher to book

更新到数据库中

在运行:python manage.py migrate

$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, books, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying books.0001_initial... OK

5.完成在数据库中建立相应表。

mysql> show tables;
+----------------------------+
| Tables_in_test             |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| books_author               |
| books_book                 |
| books_book_authors         |
| books_publisher            |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
| django_site                |
+----------------------------+
15 rows in set (0.00 sec)

 “每个数据库表对应一个类”这条规则的例外情况是多对多关系。在我们的范例模型中, Book 有一个 多对多字段 叫做 authors 。 该字段表明一本书籍有一个或多个作者,但 Book 数据库表却并没有 authors 字段。相反,Django创建了一个额外的表(多对多连接表)来处理书籍和作者之间的映射关系。

对应生成一个books_book_authors对应关系表

我们并没有显式地为这些模型定义任何主键。除非你指定,否则 Django 会自动为每个模型创建一个叫做 id 的主键。每个 Django 模型必须要有一个单列主键。

猜你喜欢

转载自km-moon11.iteye.com/blog/2352309