修改模型
models.py
1 | from django.contrib.auth.models import User |
迁移数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17python manage.py makemigrations myapp
You are trying to add a non-nullable field 'owner' to topic without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
>> 1
Migrations for 'myapp':
myapp\migrations\0003_topic_owner.py
- Add field owner to topic1
2
3
4
5
6python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, myapp, sessions
Running migrations:
Applying myapp.0003_topic_owner... OK
注意
当前,不管你以哪个用户的身份登录,都能够看到所有的主题。
只向用户显示属于自己的主题
比如 /topics.html 页面在 views.py
函数中修改:
1 | --snip-- |
保护单个主题
1 |
|
保护页面 edit_entry
1 |
|
修改“添加主题”页面
指定owner字段。
1 |
|
保护 new_entry 页面
1 |
|