我正在尝试完成 Django 教程,并且在我尝试保存和打印对象的实例变量之前,我的代码中的所有内容都可以正常工作。
这是我的课程的代码(在我的应用程序的 models.py 文件中):
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
然后我在终端中输入:
$ python manage.py sql polls
返回此输出:
BEGIN;
CREATE TABLE `polls_question` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`question_text` varchar(200) NOT NULL,
`pub_date` datetime NOT NULL
)
;
CREATE TABLE `polls_choice` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`question_id` integer NOT NULL,
`choice_text` varchar(200) NOT NULL,
`votes` integer NOT NULL
)
;
ALTER TABLE `polls_choice` ADD CONSTRAINT `question_id_refs_id_f3f98eca` FOREIGN KEY (`question_id`) REFERENCES `polls_question` (`id`);
COMMIT;
然后我将它输入到我的终端中:
$ python manage.py syncdb
这会返回:
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
然后我输入这个终端来启动python shell:
$ python manage.py shell
这是终端内的输入/输出:
In [1]: from polls.models import Question, Choice
In [2]: Question.objects.all()
Out[2]: []
In [3]: from django.utils import timezone
In [4]: q = Question(question_text="What's new?", pub_date=timezone.now())
In [5]: q.save
Out[5]: <bound method Question.save of <Question: Question object>>
#tutorial does not show this above line appearing
In [6]: q.id
#tutorial says I should get an output from this
我的问题:
1) 为什么我看到“< bound method Question.save of >”?
2) 为什么我调用对象的 id 时没有得到任何输出?
谢谢。