Skip to content

Commit 128a579

Browse files
committed
Merge branch '07-personalize-views'
2 parents 898bae3 + abad133 commit 128a579

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

Readme.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ Modify models.py and admin.py to contain your first model.
5050
Apply changes to DB:
5151
```
5252
$ ./manage.py makemigrations note
53-
$ ./manage.py migrate
53+
$ ./manage.py migrate
5454
```
5555

5656
You can now view this in the admin.
5757

5858

59-
Chapter 03 - customize the admin
59+
Chapter 03 - Customize the admin
6060
----------
6161

6262
Modify the `note/admin.py` file and see how the admin can be customized.
@@ -82,4 +82,18 @@ views use the `@login_required` decorator.
8282
Chapter 06 - Better templates and new users
8383
-----------------------------
8484

85-
Copy in templates and add in urls for creating new users.
85+
Copy in templates and add in urls for creating new users.
86+
87+
88+
Chapter 07 - Associating notes with owners
89+
-------------------------
90+
91+
Add an `owner` field to the model. You'll need to run migrations. You can use
92+
a default value of 0 for the migration.
93+
94+
```
95+
$ ./manage.py makemigrations note
96+
$ ./manage.py migrate
97+
```
98+
99+
Then, restrict the view on the index page so that a user can only see their own notes.

elevennote/note/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .models import Note
33

44
class NoteAdmin(admin.ModelAdmin):
5-
list_display = ('title', 'pub_date', 'was_published_recently')
5+
list_display = ('title', 'owner', 'pub_date', 'was_published_recently')
66
list_filter = ['pub_date']
77

88
# Register your models here.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
from django.conf import settings
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
12+
('note', '0001_initial'),
13+
]
14+
15+
operations = [
16+
migrations.AddField(
17+
model_name='note',
18+
name='owner',
19+
field=models.ForeignKey(default=0, to=settings.AUTH_USER_MODEL),
20+
preserve_default=False,
21+
),
22+
]

elevennote/note/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
# Django imports
55
from django.db import models
66
from django.utils import timezone
7-
7+
from django.contrib.auth.models import User
88

99
# Create your models here.
1010
class Note(models.Model):
11+
owner = models.ForeignKey(User)
1112
title = models.CharField(max_length=200)
1213
body = models.TextField()
1314
pub_date = models.DateTimeField('date published')

elevennote/note/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
@login_required
99
def index(request):
10-
latest_note_list = Note.objects.order_by('-pub_date')[:5]
10+
latest_note_list = Note.objects.filter(owner=request.user).order_by('-pub_date')[:5]
1111
context = {
1212
'latest_note_list': latest_note_list,
1313
}

0 commit comments

Comments
 (0)