You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: django_orm/README.md
+36-36Lines changed: 36 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,66 +12,66 @@ It's the easiest to learn by example. Let's try this, shall we?
12
12
13
13
Open up your console and type this command:
14
14
15
-
> $ python manage.py shell
15
+
> $ python manage.py shell
16
16
17
17
The effect should be like this:
18
18
19
-
(InteractiveConsole)
20
-
>>>
19
+
(InteractiveConsole)
20
+
>>>
21
21
22
22
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.
23
23
24
24
### All objects
25
25
26
26
Let's try to display all of our posts first. You can do it with the following command:
27
27
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
32
32
33
33
Oops! An error showed up. It tells us that there is no Post. It's correct -- we forgot to import it first!
34
34
35
-
>>> from blog.models import Post
35
+
>>> from blog.models import Post
36
36
37
37
This is simple: we import model `Post` from `blog.models`. Let's try displaying all posts again:
38
38
39
-
>>> Post.objects.all()
40
-
[]
39
+
>>> Post.objects.all()
40
+
[]
41
41
42
42
It's an empty list. That seems to be correct, right? We didn't add any post yet! Let's fix that.
43
43
44
44
### Create object
45
45
46
46
This is how you create a Post object in database:
47
47
48
-
>>> Post.objects.create(author = user, title = 'Sample title', text = 'Test')
48
+
>>> Post.objects.create(author = user, title = 'Sample title', text = 'Test')
49
49
50
50
But we have one missing ingredient here: `user`. We need to pass an instance of `User` model as an author. How to do that?
51
51
52
52
Let's import User model first:
53
53
54
-
>>> from django.contrib.auth.models import User
54
+
>>> from django.contrib.auth.models import User
55
55
56
56
What users do we have in our database? Try this:
57
57
58
-
>>> User.objects.all()
59
-
[<User: ola>]
58
+
>>> User.objects.all()
59
+
[<User: ola>]
60
60
61
61
Cool! Let's get an instance of the user now:
62
62
63
-
user = User.objects.get(username='ola')
63
+
user = User.objects.get(username='ola')
64
64
65
65
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.
66
66
67
67
Now we can finally create our first post:
68
68
69
-
>>> Post.objects.create(author = user, title = 'Sample title', text = 'Test')
69
+
>>> Post.objects.create(author = user, title = 'Sample title', text = 'Test')
70
70
71
71
Hurray! Wanna check if it worked?
72
72
73
-
>>> Post.object.all()
74
-
[<Post: Sample title>]
73
+
>>> Post.object.all()
74
+
[<Post: Sample title>]
75
75
76
76
### Add more posts
77
77
@@ -81,48 +81,48 @@ You can know have a little fun and add more posts to see how it works. Add 2-3 m
81
81
82
82
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:
83
83
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>]
86
86
87
87
Or maybe we want to see all the posts that contain a word 'title' in the `title` field?
0 commit comments