(lyb) l@ubuntu:~/Documents$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
(lyb) l@ubuntu:~/Documents$ ls
db.sqlite3 learning_log lyb manage.py
1.8 查看项目
(lyb) l@ubuntu:~/Documents$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
November 04, 2017 - 16:13:50
Django version 1.11.7, using settings 'learning_log.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
18.2 创建应用程序
重新打开一个终端:
from django.db import models
# Create your models here.
class Topic(models.Model):
'''用户学习的主题'''
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
'返回模型的字符串表示
return self.text
2.2 激活模型
打开/home/l/Documents/learning_log中的setting.py,在INSTALLED_APPS中添加 ‘learning_logs’
继续命令行:
(lyb) l@ubuntu:~/Documents$ python manage.py makemigrations learning_logs
Migrations for 'learning_logs':
learning_logs/migrations/0001_initial.py
- Create model Topic
(lyb) l@ubuntu:~/Documents$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, learning_logs, sessions
Running migrations:
Applying learning_logs.0001_initial... OK
每当需要修改‘学习笔记’管理的数据时,都采取如下三个步骤:修改models.py;对learning_logs调用makemigrations;让Django迁移项目。
2.3 Django管理网站
1 创建超级用户
(lyb) l@ubuntu:~/Documents$ python manage.py createsuperuser
Username (leave blank to use 'l'): lyb
Email address:
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
Password:
Password (again):
Superuser created successfully.
2. 向管理网站注册模型
打开models.py所在目录中的admin.py,输入:
from django.contrib import admin
# Register your models here.
from learning_logs.models import Topic
admin.site.register(Topic)
打开浏览器访问http://localhost:8000/admin/,输入超级用户名及密码后你会看到:
3. 添加主题
2.4 定义模型Entry
models.py
class Entry(models.Model):
'''学到的有关某个主题的具体知识'''
topic = models.ForeignKey(Topic)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def __str__(self):
'返回模型的字符串表示'
return self.text[:50] + '...'
2.5 迁移模型Entry
(lyb) l@ubuntu:~/Documents$ python manage.py makemigrations learning_logs
Migrations for 'learning_logs':
learning_logs/migrations/0002_entry.py
- Create model Entry
(lyb) l@ubuntu:~/Documents$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, learning_logs, sessions
Running migrations:
Applying learning_logs.0002_entry... OK
2.6 向管理网站注册Entry
admin.py
from django.contrib import admin
# Register your models here.
from learning_logs.models import Topic,Entry
admin.site.register(Topic)
admin.site.register(Entry)
2.7 Django shell
(lyb) l@ubuntu:~/Documents$ python manage.py shell
Python 3.5.2 (default, Sep 14 2017, 22:51:06)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from learning_logs.models import Topic
>>> Topic.objects.all()
<QuerySet [<Topic: chess>, <Topic: Rock Clibing>]> # QuerySet 查询集