Django doc summary (2)

Database

Default database

In the project setting file 'samsite/settings.py', there are some lines' code about the db.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

The default db is sqlite3. You can select another db which you like, but you should modify some settings, such as 'engine', user, password, etc.

Creat models

samapp/models.py

from django.db import models

# Create your models here.
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

So, we have two models and each model has some fileds.

Activating models

Edit the samsite/settings.py to let Django know to include the samapp app.

INSTALLED_APPS = [
    'samapp.apps.SamappConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

By running makemigrations, you're telling Django that you have some changes to your models and that you'd like the changes to be stored as a migration.

D:\SamProject\django\samsite>python manage.py makemigrations samapp
Migrations for 'samapp':
  0001_initial.py:
- Create model Choice
- Create model Question
- Add field question to choice

As we use sqlite, you can use below cmd to view the sql.

D:\SamProject\django\samsite>python manage.py sqlmigrate samapp 0001
BEGIN;
--
-- Create model Choice
--
CREATE TABLE "samapp_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL);
--
-- Create model Question
--
CREATE TABLE "samapp_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(200) NOT NULL, "pub_date" datetime NOT NULL);
--
-- Add field question to choice
--
ALTER TABLE "samapp_choice" RENAME TO "samapp_choice__old";
CREATE TABLE "samapp_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choice_text" varchar(200) NOT NULL, "votes" integer NOT NULL, "question_id" integer NOT NULL REFERENCES "samapp_question" ("id"));
INSERT INTO "samapp_choice" ("choice_text", "id", "votes", "question_id") SELECT "choice_text", "id", "votes", NULL FROM "samapp_choice__old";
DROP TABLE "samapp_choice__old";
CREATE INDEX "samapp_choice_7aa0f6ee" ON "samapp_choice" ("question_id");

COMMIT;

Finally, we use the bellow cmd to create those model tables in your db.

D:\SamProject\django\samsite>python manage.py migrate
Operations to perform:
  Apply all migrations: sessions, admin, auth, contenttypes, samapp
Running migrations:
  Rendering model states... DONE
  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 samapp.0001_initial... OK
  Applying sessions.0001_initial... OK
posted @ 2015-12-10 22:31  sam_rui  阅读(178)  评论(0编辑  收藏  举报