django1.7 Summary of points that I got stuck in doing the tutorial https://docs.djangoproject.com/en/1.7/intro/tutorial01/
Note that the commands up to migration are different. The following is the method in 1.7.
mysite/setting.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
Adding polls application to django
Execute the following command
-Creation of migration file
$ python manage.py makemigrations polls
When executed normally, it will be as follows
Migrations for 'polls':
0001_initial.py:
- Create model Question
- Create model Choice
- Add field question to choice
A file called 0001_initial.py is created and The question and choice models are defined in it. This is the migration file.
-Creating SQL for migration
$ python manage.py sqlmigrate polls 0001
If this is successful, you will see the following
BEGIN;
CREATE TABLE polls_question (
"id" serial NOT NULL PRIMARY KEY,
"question_text" varchar(200) NOT NULL,
"pub_date" timestamp with time zone NOT NULL
);
CREATE TABLE polls_choice (
"id" serial NOT NULL PRIMARY KEY,
"question_id" integer NOT NULL,
"choice_text" varchar(200) NOT NULL,
"votes" integer NOT NULL
);
CREATE INDEX polls_choice_7aa0f6ee ON "polls_choice" ("question_id");
ALTER TABLE "polls_choice"
ADD CONSTRAINT polls_choice_question_id_246c99a640fbbd72_fk_polls_question_id
FOREIGN KEY ("question_id")
REFERENCES "polls_question" ("id")
DEFERRABLE INITIALLY DEFERRED;
COMMIT;
・ Execution of migration
$ python manage.py migrate
polls/model.py
from django.db import models
class Question(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.question_text
class Choice(models.Model):
# ...
def __str__(self): # __unicode__ on Python 2
return self.choice_text
This way the interactive shell should look like this
>>> Question.objects.all()
[<Question: What's up?>]
It will be as follows
>>> Question.objects.all()
[<Question: Question object>]
Note: The data executed by the interactive shell is saved in the db as it is.
**[Solution] After updating model.py, it will be reflected if you exit the interactive shell once and start the interactive shell again. ** **
Recommended Posts