はじめての Django アプリ作成、その 1 (2)

昨晩呑み杉で gdgd。とりあえず中断点から再開。
とりあえず sqlite3 で、な方向。あと、参考コンテンツによれば INSTALLED_APPS に関する記述あり。現時点では以下

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
)

認証、content-type の処理(??)、セッション、複数のサイト管理なフレームワークとの事。以降でアプリケーションを作っていくらしい。手順としては

  • アプリケーション作成
  • モデル作成
  • REPL(とは言わんか)で DBAPI を試す

との事。最後のソレは参考まで、という事でしょうか以下でトレイスなナニを。

アプリケーション作成からモデル作成

$ python manage.py startapp polls
$ ls
__init__.py  __init__.pyc  manage.py  mysite.db  polls  settings.py  settings.pyc  settings.py~  urls.py  urls.pyc
$ ls polls/
__init__.py  models.py  views.py
$ 

で polls/models.py を開け、とある。デフォルトは以下の状態

from django.db import models

# Create your models here.

コピペしても良かったんでしょうが、手で入力してみた。

from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choise = models.CharField(max_length=200)
    votes = models.IntegerField()

scheme みたく_シメ_な括弧とかあればわかりやすいのに、と言いつつ (何
あまり python の文法分かってないのでテキストに書いてあるナニを以下に控えておきます。

  • Poll も Choice も models.Model のサブクラス
  • question と pub_date は Poll の属性 (Choice は略
  • 属性は models.*Field のインスタンス
  • pub_date が参照するオブジェクトは introspection のための情報がある模様
  • models.ForeignKey によってリレーションを張っている模様

モデルの定義が上記で良ければ INSTALLED_APPS な設定変更して

$ python manage.py sql polls
mysite.polls: __init__() got an unexpected keyword argument 'max_length'
1 error found.
Traceback (most recent call last):
  File "manage.py", line 11, in ?
    execute_manager(settings)
  File "/usr/lib/python2.4/site-packages/django/core/management.py", line 1672, in execute_manager
    execute_from_command_line(action_mapping, argv)
  File "/usr/lib/python2.4/site-packages/django/core/management.py", line 1620, in execute_from_command_line
    mod_list = [models.get_app(app_label) for app_label in args[1:]]
  File "/usr/lib/python2.4/site-packages/django/db/models/loading.py", line 40, in get_app
    mod = load_app(app_name)
  File "/usr/lib/python2.4/site-packages/django/db/models/loading.py", line 51, in load_app
    mod = __import__(app_name, {}, {}, ['models'])
  File "/home/guest/django/mysite/../mysite/polls/models.py", line 3, in ?
    class Poll(models.Model):
  File "/home/guest/django/mysite/../mysite/polls/models.py", line 4, in Poll
    question = models.CharField(max_length=200)
TypeError: __init__() got an unexpected keyword argument 'max_length'
$

あらら。何だろね。困ったなぁ、と言いつつ一服して SD のナニを見てると max_length ではなくて maxlength となっておるので修正してリトライしてみると

$ python manage.py sql polls
BEGIN;
CREATE TABLE "polls_poll" (
    "id" integer NOT NULL PRIMARY KEY,
    "question" varchar(200) NOT NULL,
    "pub_date" datetime NOT NULL
);
CREATE TABLE "polls_choice" (
    "id" integer NOT NULL PRIMARY KEY,
    "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id"),
    "choise" varchar(200) NOT NULL,
    "votes" integer NOT NULL
);
COMMIT;
$

を、通った。色付き。そうか、リレーションは id でヤるのか。manage.py は他にも

  • validate
  • sqlcustom
  • sqlclear
  • sqlindexes
  • sqlall

などが渡せる、とある。実際には syncdb で差分のみ盛り込まれるとの事。だけど model のみ修正した時とかはどうなるんだろうか。後で出てくるかな。

$ python manage.py syncdb
Creating table polls_poll
Creating table polls_choice
Installing index for polls.Choice model
Loading 'initial_data' fixtures...
No fixtures found.
$

API で遊ぶ

テキストを色々とナゾる。

$ python manage.py shell
Python 2.4.4 (#2, Apr  5 2007, 20:11:18)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from mysite.polls.models import Poll, Choice
>>> Poll.objects.all()
[]
>>> import datetime
>>> p = Poll(question="What's up?", pub_date=datetime.datetime.now())
>>> p.save()
>>> p.id
1
>>> p.question
"What's up?"
>>> p.pub_date
datetime.datetime(2008, 4, 19, 20, 29, 29, 207972)
>>> p.pub_date = datetime.datetime(2008, 4, 1, 0, 0)
>>> p.save()
>>> Poll.objects.all()
[<Poll: Poll object>]
# ココで __unicode__ を仕込む
>>> from mysite.polls.models import Poll, Choice
>>> Poll.objects.all()
[<Poll: Poll object>]
>>> 

駄目ぢゃん、と言いつつバージョン確認してみようとしつつ具体的な方法が分からなくって自分のログを確認してみたら 0.96.1 となっていた。
あまり余計な部分を気にせずに_その 4_までがっつりトレイスしたいんだけどな。