Skip to content

Commit b757a72

Browse files
committed
Removed tabs in django_orm chapter.
There shouldn't be any left now.
1 parent f95138d commit b757a72

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

django_orm/README.md

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,66 @@ It's the easiest to learn by example. Let's try this, shall we?
1212

1313
Open up your console and type this command:
1414

15-
> $ python manage.py shell
15+
> $ python manage.py shell
1616

1717
The effect should be like this:
1818

19-
(InteractiveConsole)
20-
>>>
19+
(InteractiveConsole)
20+
>>>
2121

2222
You're now in Django's interactive console. It's just like Python prompt but with some additional Django magic :) You can use all Python commands here too, of course.
2323

2424
### All objects
2525

2626
Let's try to display all of our posts first. You can do it with the following command:
2727

28-
>>> Post.objects.all()
29-
Traceback (most recent call last):
30-
File "<console>", line 1, in <module>
31-
NameError: name 'Post' is not defined
28+
>>> Post.objects.all()
29+
Traceback (most recent call last):
30+
File "<console>", line 1, in <module>
31+
NameError: name 'Post' is not defined
3232

3333
Oops! An error showed up. It tells us that there is no Post. It's correct -- we forgot to import it first!
3434

35-
>>> from blog.models import Post
35+
>>> from blog.models import Post
3636

3737
This is simple: we import model `Post` from `blog.models`. Let's try displaying all posts again:
3838

39-
>>> Post.objects.all()
40-
[]
39+
>>> Post.objects.all()
40+
[]
4141

4242
It's an empty list. That seems to be correct, right? We didn't add any post yet! Let's fix that.
4343

4444
### Create object
4545

4646
This is how you create a Post object in database:
4747

48-
>>> Post.objects.create(author = user, title = 'Sample title', text = 'Test')
48+
>>> Post.objects.create(author = user, title = 'Sample title', text = 'Test')
4949

5050
But we have one missing ingredient here: `user`. We need to pass an instance of `User` model as an author. How to do that?
5151

5252
Let's import User model first:
5353

54-
>>> from django.contrib.auth.models import User
54+
>>> from django.contrib.auth.models import User
5555

5656
What users do we have in our database? Try this:
5757

58-
>>> User.objects.all()
59-
[<User: ola>]
58+
>>> User.objects.all()
59+
[<User: ola>]
6060

6161
Cool! Let's get an instance of the user now:
6262

63-
user = User.objects.get(username='ola')
63+
user = User.objects.get(username='ola')
6464

6565
As you can see, we know `get` a `User` with a `username` that equals to 'ola'. Neat! Of course, you have to adjust it to your username.
6666

6767
Now we can finally create our first post:
6868

69-
>>> Post.objects.create(author = user, title = 'Sample title', text = 'Test')
69+
>>> Post.objects.create(author = user, title = 'Sample title', text = 'Test')
7070

7171
Hurray! Wanna check if it worked?
7272

73-
>>> Post.object.all()
74-
[<Post: Sample title>]
73+
>>> Post.object.all()
74+
[<Post: Sample title>]
7575

7676
### Add more posts
7777

@@ -81,48 +81,48 @@ You can know have a little fun and add more posts to see how it works. Add 2-3 m
8181

8282
Big part of QuerySets is an ability to filter them. Let's say, we want to find all posts that are authored by User ola. We will use `filter` instead of `all` in `Post.objects.all()`. In parentheses we will state what condition(s) needs to be met by a blog post to end up in our queryset. In our situation it is `author` that is equal to `user`. The way to write it in Django is: `author=user`. Now our piece of code looks like that:
8383

84-
>>> Post.objects.filter(author = user)
85-
[<Post: Sample title>, <Post: Post number 2>, <Post: My 3rd post!>, <Post: 4th title of post>]
84+
>>> Post.objects.filter(author = user)
85+
[<Post: Sample title>, <Post: Post number 2>, <Post: My 3rd post!>, <Post: 4th title of post>]
8686

8787
Or maybe we want to see all the posts that contain a word 'title' in the `title` field?
8888

89-
>>> Post.objects.filter(title__contains = 'title')
90-
[<Post: Sample title>, <Post: 4th title of post>]
89+
>>> Post.objects.filter(title__contains = 'title')
90+
[<Post: Sample title>, <Post: 4th title of post>]
9191

9292
You can also get a list of all published posts. We do it by filtering all the posts that have `published_date`:
9393

94-
>>> Post.objects.filter(published_date__isnull=False)
95-
[]
94+
>>> Post.objects.filter(published_date__isnull=False)
95+
[]
9696

9797
Unfortunately, none of our posts is published now. We can change that! First get an instance of a post we want to publish:
9898

99-
>>> post = Post.objects.get(id = 1)
99+
>>> post = Post.objects.get(id = 1)
100100

101101
And then publish it with our `publish` method!
102102

103-
>>> post.publish()
103+
>>> post.publish()
104104

105105
Now try to get list of published posts again (press the up arrow button 3 times and hit Enter):
106106

107-
>>> Post.objects.filter(published_date__isnull=False)
108-
[<Post: Sample title>]
107+
>>> Post.objects.filter(published_date__isnull=False)
108+
[<Post: Sample title>]
109109

110110
### Ordering objects
111111

112112
QuerySets also allow you to order the list of objects. Let's try to order them by `created_date` field:
113-
114-
>>> Post.objects.all().order_by('created_date')
115-
[<Post: Sample title>, <Post: Post number 2>, <Post: My 3rd post!>, <Post: 4th title of post>]
113+
114+
>>> Post.objects.all().order_by('created_date')
115+
[<Post: Sample title>, <Post: Post number 2>, <Post: My 3rd post!>, <Post: 4th title of post>]
116116

117117
We can also reverse the ordering by adding `-` at the beggining:
118-
119-
>>> Post.objects.all().order_by('-created_date')
120-
[<Post: 4th title of post>, <Post: My 3rd post!>, <Post: Post number 2>, <Post: Sample title>]
118+
119+
>>> Post.objects.all().order_by('-created_date')
120+
[<Post: 4th title of post>, <Post: My 3rd post!>, <Post: Post number 2>, <Post: Sample title>]
121121

122122
Cool! You're now ready for the next part! To close shell, type this:
123123

124-
>>> exit()
125-
$
124+
>>> exit()
125+
$
126126

127127

128128

0 commit comments

Comments
 (0)