-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell.txt
114 lines (105 loc) · 2.73 KB
/
shell.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
>>> from blog.models import Post
>>> from django.contrib.auth.models import User
>>> User.objects.all()
<QuerySet [<User: boris>]>
>>> User.objects.first()
<User: boris>
>>> User.objects.filter(username='Boris')
<QuerySet []>
>>> User.objects.filter(username='boris')
<QuerySet [<User: boris>]>
>>> User.objects.filter(username='boris').first()
<User: boris>
>>> user = User.objects.filter(username='boris').first()
>>> user
<User: boris>
>>> user.id
1
>>> user.pk
1
>>> user = User.objects.get(id=1)
>>> user
<User: boris>
>>> Post.objects.all()
<QuerySet []>
>>> post_1 = Post(title='blog 1', content='First Post Content!', author = user)
>>> Post.objects.all()
<QuerySet []>
>>> post_1.save()
>>> Post.objects.all()
<QuerySet [<Post: Post object (1)>]>
>>> exit()
>>> from blog.models import Post
>>> from django.contrib.auth.models import User
>>> Post.objects.all()
<QuerySet [<Post: blog 1>]>
>>> user = User.objects.filter(username = 'boris').first()
>>> user
<User: boris>
>>> post_2 = Post(title = 'Blog 2', content = 'Second Post Content!', author_id = user.id)
>>> post_2.save()
>>> Post.objects.all()
<QuerySet [<Post: blog 1>, <Post: Blog 2>]>
>>> post = Post.objects.first()
>>> post.content
'First Post Content!'
>>> post.date_posted
datetime.datetime(2022, 7, 9, 6, 20, 27, 788956, tzinfo=datetime.timezone.utc)
>>> post.author
<User: boris>
>>> post.author.email
'boris@gmail.com'
>>> user.post_set
<django.db.models.fields.related_descriptors.create_reverse_many_to_one_manager.<locals>.RelatedManager object at 0x000002038F605F90>
>>> user.post_set.all()
<QuerySet [<Post: blog 1>, <Post: Blog 2>]>
>>> user.post_set.create(title='Blog 3', content = 'Third Post Content!')
<Post: Blog 3>
>>> Post.objects.all()
<QuerySet [<Post: blog 1>, <Post: Blog 2>, <Post: Blog 3>]>
>>> from django.contrib.auth.models import User
>>> user = User.objects.filter(username='boris').first()
>>> user
<User: boris>
>>> user.profile
<Profile: boris Profile>
>>> user.profile.image
<ImageFieldFile: profile_pics/03c89dbf0325753a847ed914f490ff6d.jpg>
>>> user.profile.image.width
850
>>> user.profile.image.url
'/profile_pics/03c89dbf0325753a847ed914f490ff6d.jpg'
>>> import json
>>> from blog.models import Post
>>> with open('posts.json') as f:
... posts_json = json.load(f)
...
>>> for post in posts_json:
... post = Post(title=post['title'], content = post['content'], author_id=post['user_id'])
... post.save()
...
>>> from django.core.paginator import Paginator
>>> posts = ['1','2','3','4','5']
>>> p = Paginator(posts, 2)
>>> p.num_pages
3
>>> for page in p.page_range:
... print(page)
...
1
2
3
>>> p1 = p.page(1)
>>>
>>> p1
<Page 1 of 3>
>>> p1.number
1
>>> p1.objects_list
['1','2']
>>> p1.has_previous()
False
>>> p1.has_next()
True
>>> p1.next_page_number()
2