标签:
刚开始学django,用的是django book 2.0的教程。感觉这个教程比官方文档更容易入门,但因为这个教程针对的版本有些低,而我下的是django 1.8的版本。所以还是边看边练的时候还是有些坑啊。把遇到的问题在这里记录下
一、激活app
在mysite目录下,建立新的app。
$ python manage.py startapp books
在setting.py中,找到INSTALLED_APPS,把新建的APP,books加进去,如下。《django book2.0》中,是"mysite.books",在1.8中这样加会报错,找不到该应用
INSTALLED_APPS = (
#‘django.contrib.admin‘,
#‘django.contrib.auth‘,
#‘django.contrib.contenttypes‘,
#‘django.contrib.sessions‘,
#‘django.contrib.messages‘,
#‘django.contrib.staticfiles‘,
‘books‘, )
二、建立模型
1.编辑完models.py后,验证模型是否准确。应使用python manage.py check。
$ python manage.py validate
/usr/local/lib/python2.7/distpackages/Django-1.8.8-py2.7.egg/django/core/management/commands/validate.py:15: RemovedInDjango19Warning: "validate" has
been deprecated in favor of "check". RemovedInDjango19Warning)
2.查看模型models.py会在数据库中运行怎样的sql,使用 python manage.py sqlall books 会报如下错误
CommandError: App ‘books‘ has migrations. Only the sqlmigrate and sqlflush commands can be used when an app has migrations.
正确的做法是
$ python manage.py makemigrations books
Migrations for ‘books‘:
0001_initial.py:
- Create model Author
- Create model Book
- Create model Publisher
- Add field publisher to book
$ python manage.py sqlmigrate books 0001
BEGIN;
CREATE TABLE `books_author` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(60) NOT NULL, `email` varchar(254) NOT NULL);
.....
$ python manage.py migrate
Operations to perform:
Apply all migrations: books
Running migrations:
Rendering model states... DONE
Applying books.0001_initial... OK
django初用的一些问题
标签:
原文地址:http://www.cnblogs.com/ymiao/p/5125224.html