【django】【基础】models

2018-04-27  本文已影响70人  JerichoPH

models

创建模型:

class Article(models.Model):
    title = models.CharField(max_length=32, default='title')
    content = models.CharField(max_length=255, null=True)

创建数据库迁移:

Migrations for 'supporter':
  supporter\migrations\0001_initial.py:
    - Create model Article

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

==完成迁移==

完成迁移后续:

# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2018-04-27 13:22
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Article',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(default='title', max_length=32)),
                ('content', models.CharField(max_length=255, null=True)),
            ],
        ),
    ]

BEGIN;
--
-- Create model Article
--
CREATE TABLE "supporter_article" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(32) NOT NULL, "content" varchar(255) NULL);
COMMIT;

读取数据

<h1>title:" {{ data.title }} "</h1>
<h2>content:" {{ data.content }} "</h2>
上一篇下一篇

猜你喜欢

热点阅读