Stumble when doing the django 1.7 tutorial

django1.7 Summary of points that I got stuck in doing the tutorial https://docs.djangoproject.com/en/1.7/intro/tutorial01/

Differences in model settings between django 1.4 and 1.7 [Unresolved]

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

Writing unicode settings in model.py doesn't work [solved]

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. ** **

>>> What is p.was_published_recently () doing? [unsolved]

Recommended Posts

Stumble when doing the django 1.7 tutorial
Deploy the Django tutorial to IIS ①
A note about doing the Pyramid tutorial
Python Django Tutorial (5)
Python Django Tutorial (2)
django tutorial memo
Python Django Tutorial (8)
Python Django Tutorial (6)
Start Django Tutorial 1
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
Python Django Tutorial (3)
Python Django Tutorial (4)
I referred to it when I got stuck in the django geodjango tutorial (editing)
Python Django tutorial summary
Django Polymorphic Associations Tutorial
What I did when I stumbled on a Django tutorial
[Django] Rename the project
Django Girls Tutorial Note
Until the start of the django tutorial with pycharm on Windows
Django --Overview the tutorial app on Qiita and add features (2)
Get started with Django! ~ Tutorial ④ ~
DEBUG settings when using Django
Get started with Django! ~ Tutorial ⑥ ~
Python Django Tutorial Cheat Sheet