-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrf.html
executable file
·437 lines (384 loc) · 14.3 KB
/
drf.html
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- base href="http://thayer.b2si.com/" -->
<link rel="icon" href="images/cs-favicon.ico" type="image/x-icon" />
<title>Django DRF Cheat Sheet</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
crossorigin="anonymous">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<!-- sumo -->
<script async>(function(s,u,m,o,j,v){j=u.createElement(m);v=u.getElementsByTagName(m)[0];j.async=1;j.src=o;j.dataset.sumoSiteId='22cb51ba7caae4c7787809bad98ea8216c342a4720087f1f1bde272f364ff494';v.parentNode.insertBefore(j,v)})(window,document,'script','//load.sumo.com/');</script>
<!-- adsense support (login: denmark@ny.com) -->
<script data-ad-client="ca-pub-6143770949021679" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-1660181-11"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-1660181-11');
</script>
<style>
body, td, p {
font-family: Verdana, Arial, Helvetica, Chicago, sans-serif;
font-size: 12px;
}
td {
padding: 0.25em;
}
pre {
font-size: 11px;
margin: 0px;
padding: 0.25em;
}
.sh {
font-size: 14px;
font-weight: bold;
}
h3 {
font-size: 12px;
font-weight: bold;
margin: 0.25em 0px 0.25em 0px;
}
h4 {
font-weight: bold;
}
ul {
padding-inline-start: 1em;
}
.panel {
background-color: transparent;
}
.panel-primary>.panel-heading {
background-image: none;
background-color: #0066CC;
border-color: #0066CC;
}
.panel-primary {
margin-bottom: 0.5em;
}
.panel-heading {
padding: 0.5em;
}
.panel-body {
padding: 0.5em;
background-color: transparent;
}
</style>
</head>
<body bgcolor="#FFFFFF" background="images/hex-bg-grid.gif">
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1 style="font-size: 14pt;"><b>Django DRF Cheat Sheet</b>
<a href="http://thayer.b2si.com/"><img src="icons-thayer/document.gif" align=right></a>
</h1>
<hr noshade style="height: 1px;">
</div>
</div>
<div class="row">
<div class="col-xs-12 col-md-4" style="padding: 0.5em;">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title">Django DRF Cheat Sheet</h4>
</div>
<div class="panel-body">
<p><b><i>Here's the PDF: <a href="https://bit.ly/django-drf-cheat-sheet">Django DRF
Cheat Sheet</a> v1.3, and <a href="#cheatsheet">HTML</a></i></b>
</p>
<blockquote><i>Welcome. This is a guide to the parts of DRF
(Django Rest Framework) that I routinely forget or need to
reference periodically. This is not a learning resource nor
a tutorial, but intended for those already programming in
DRF.<br>
- <a href="http://thayer.b2si.com/">Charles Thayer</a>
</i></blockquote>
<p>Got constructive feedback? <a href="https://forms.gle/QN5fWpgGFZXBCfy96">Let me know</a><br>
(Want a new or different cheat sheet? DRF Basics, Django, CBV, Firebase?)
</p>
</div>
</div>
</div>
<div class="col-xs-12 col-md-4" style="padding: 0.5em;">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title">Info</h4>
</div>
<div class="panel-body">
<ul>
<!-- drf-cheatsheet.pdf -->
<li>PDF document: <a href="https://bit.ly/django-drf-cheat-sheet">Django DRF Cheat
Sheet</a> v1.3</li>
<li>Official site: <a href="https://www.django-rest-framework.org/">Django REST Framework</a></li>
<li>Official tutorial: <a href="https://www.django-rest-framework.org/tutorial/quickstart/">Quickstart Tutorial</a></li>
<li>Real Python's single-page tutorial: <a href="https://realpython.com/django-rest-framework-quick-start/">Django Rest Framework – An Introduction</a></li>
<li>Sunscrapers' multi-part tutorial: <a href="https://sunscrapers.com/blog/ultimate-tutorial-django-rest-framework-part-1/">The Ultimate Tutorial for Django REST Framework: CRUD (Part 1)</a></li>
<li>Agiliq's book for learners: <a href="https://books.agiliq.com/projects/django-api-polls-tutorial/en/latest/">Building APIs with Django REST Framework</a></li>
<li>GitLab Repo for cheat sheet code: <a href="https://gitlab.com/cgthayer/drf-cheatsheet">drf-cheatsheet</a></li>
</ul>
</div>
</div>
</div>
<div class="col-xs-12 col-md-4" style="padding: 0.5em;">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title">About</h4>
</div>
<div class="panel-body">
<p>
I've been using Django and DRF off and on for way too
long. Lately, I've been working as the CTO of a startup in
San Francisco that built a mobile app for "managed
messaging" for businesses and professionals. That project
incorporated a fair amount of API/Javascript communication,
which lead me to take notes on the parts of DRF that I
needed to review periodically.</p>
<p>
I hope you find it helpful and provide constructive
feedback. If enough people do, I'll continue to update
and distribute it. Thanks.</p>
-- <a href="http://thayer.b2si.com">Charles Thayer</a> (thayer at b2si com)
</div>
</div>
</div>
</div>
<!-- ---------------------------------------------------------------------- -->
<a name="cheatsheet"></a>
<div class="row">
<!-- left -->
<div class="col-xs-12 col-md-4" style="padding: 0.5em;">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title">Serializers</h4>
</div>
<div class="panel-body">
<h3>Serialize:</h3>
<p>
<img src="images/serializer-code.png" width="160px"/>
<pre># Object instance -> Data dict
s = Serializer(instance)
s.data</pre></p>
<h3>Deserialize (create):</h3>
<p>
<img src="images/deserializer-code.png" width="160px"/>
<pre># dict -> Object
s = Serializer(data={..})
s.is_valid()
instance = s.save()</pre></p>
<h3>Validation:</h3>
<pre>
s = Serializer(data={..})
if s.is_valid(raise_exception=False):
s.validated_data
else:
s.errors # dict(field=[problems..])</pre>
<h3>Update:</h3>
<pre>
s = Serializer(instance, validated_data, partial=True)
new = s.save()</pre>
<h3>Serializer Classes:</h3>
<pre>
class SimpleSerializer(serializers.Serializer):
name = serializers.CharField(max_length=128)
user = UserSerializer(required=false) # nested
...
def create(self, validated_data):
kwargs = self.context['view'].kwargs
...
return instance
def update(self, instance, validated_data):
# if self.partial
...
return instance
def save(self, **kwargs):
# instance = super().save(**kwargs)
nm = self.validated_data.get('name')
...
instance = model_object.save()
return instance
# def validate_<field>(self, name):
def validate_name(self, name):
if not name:
raise serializers.ValidationError('Name required')
...
return name
def validate(self, data):
nm = data.get('name')
if not nm:
raise serializers.ValidationError({'name': ..})
...
return validated_data
class DbSerializer(serializers.ModelSerializer):
class Meta:
model = DbModel
fields = [..]
list_serializer_class = DbListSerializer
def to_representation(self, obj) → Dict
def to_internal(self, data) → Dict # validated
</pre>
</div>
</div>
</div>
<!-- middle -->
<div class="col-xs-12 col-md-4" style="padding: 0.5em;">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title">Basics</h4>
</div>
<div class="panel-body">
<p>
<h3>Serializers</h3>
<table cellpadding="4em">
<tr><td>Create</td><td>Serializer(data={..}); s.save(): Object</td></tr>
<tr><td>Serialize</td><td>Serializer(instance); s.data: Dict</td></tr>
<tr><td>Update</td><td>Serializer(instance, data={..}, partial=True)</td></tr>
<tr><td>Set</td><td>Serializer(instance, data={..})</td></tr>
</table>
</p>
<p>
<h3>Serializer Fields:</h3>
<ul>
<li>Common: CharField, UUIDField, NullBooleanField, DateTimefield, JSONField, EmailField, ..</li>
<li>Special: SerializerMethodField, RelatedField, PrimaryKeyRelatedField, SlugRelatedField, HyperlinkedRelatedField..</li>
</ul>
</p>
<p>
<h3>Field Args</h3>
<table border="1px">
<tr><th></th><th>Default</th><th>Input</th><th>Output</th><th>Notes</th></tr>
<tr><td>read_only</td><td>False</td><td>no</td><td>yes</td><td></td></tr>
<tr><td>write_only</td><td>False</td><td>yes</td><td>no</td><td></td></tr>
<tr><td>required</td><td>True</td><td>yes</td><td>yes</td><td>raise if missing</td></tr>
<tr><td>allow_null</td><td>False</td><td></td><td></td><td>None is allowed</td></tr>
<tr><td>default</td><td><value></td><td>sets</td><td></td><td>If set, don’t use the arg “required”</td></tr>
<tr><td>source</td><td colspan=4>the python form of field access like "user.address.zip" (not double underscore)
OR a callable name on the object like get_primary_phone()</td></tr>
</table>
</p>
<p>
<h3>Request</h3>
<ul>
<li>request.data - dict of (post, put, patch)</li>
<li>request.query_param (get)</li>
<li>request.user (django.contrib.auth.models.AnonymousUser)</li>
<li>request.auth (usually a model instance)</li>
<li>HTTP stuff: request.method (e.g. GET), .content_type (e.g. application/json), .META, .headers, .path</li>
</ul>
</p>
<h3>view() functions</h3>
<pre>
# urls.py
urlpatterns = [
path(r'viewfn/<int:arg1>/', views.my_view_fn),
]</pre>
<pre># views.py
from django.http import JsonResponse
...
def my_view_fn(request, arg1=None):
if request.method == ‘POST’:
return JsonResponse({}, status=201)</pre>
<h3>Testing</h3>
<pre>
from rest_framework.test import APIClient, APITestCase
class MyTests(APITestCase):
def test_basics(self):
client = APIClient()
response = client.post(
f'/viewtest/{arg1}', data={..})
assert response.status_code == 200
assert response.json().get(‘name’) == name</pre>
<h3>Views</h3>
<pre>
class SimpleView(APIView):
def post(self, request, *args, **kwargs):
request.data.get(‘name’)
return Response(..)
def get(self, request, *args, **kwargs):
request.query_params.get(‘name’)
...
def patch(self, request, *args, **kwargs):
...
def delete(self, request, *args, **kwargs):
...</pre>
</div>
</div>
</div>
<!-- right -->
<div class="col-xs-12 col-md-4" style="padding: 0.5em;">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title">ViewSets</h4>
</div>
<div class="panel-body">
<p>
<table>
<tr valign=top>
<td><img src="images/flow-overview.png" width="80px"/></td>
<td><img src="images/flow-cbv.png" width="100px"/></td>
<td><img src="images/flow-viewset.png" width="140px"/></td>
</tr>
</table>
</p>
<h3>ViewSets (ModelViewSets)</h3>
<pre>
from rest_framework import permissions
class SimpleViewSet(ViewSet): # ModelViewSet
serializer_class = SimpleSerializer
# queryset = SimpleModel.objects.all()
lookup_field = 'field'
authentication_classes = (..,)
permissions_classes = (permissions.IsAuthenticated,)
# permissions_classes = (permissions.AllowAny,)
# filter_backends = (..,)
def create(self, request, *args, **kwargs):
return Response(..)
def update(self, request, *args, **kwargs):
def partial_update(self, request, *args, **kwargs):
def destroy(self, request, *args, **kwargs):
def retrieve(self, request, *args, **kwargs):
def list(self, request, *args, **kwargs):
def get_queryset(self):
# related to self.queryset
data = self.request.data
qp = self.request.query_params
kwargs = self.kwargs # args for view
def get_object(self): → instance
# see self.lookup_field
def get_serializer(self):
def get_serializer_context(self):
...
return dict(request=..., view=...
@detail_router([‘GET’, ‘POST’])
def custom(request, *args, **kwargs):
...</pre>
<pre># urls.py
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(
r'viewset',
example.SimpleViewSet,
basename='Simple’
)
urlpatterns = [
path(r‘’, include(router.urls)),
...</pre>
</div>
</div>
</div>
</div>
</div>