Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Hunting For Treasure In Django
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Seb
May 28, 2015
Technology
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Hunting For Treasure In Django
Seb
May 28, 2015
More Decks by Seb
See All by Seb
Double Click - Continue Building Better CLIs
elbaschid
0
500
I Can Be A Speaker, So Can You
elbaschid
0
340
Click - PyCaribbean 2017 - Puerto Rico
elbaschid
0
470
Conferencing - Engineering Meeting
elbaschid
1
53
Show & Tell - PyCon US 2016 Summary
elbaschid
1
110
Click: A Pleasure To Write, A Pleasure To Use
elbaschid
0
690
Hunting for Treasure in Django
elbaschid
1
740
Moby & The Beanstalk
elbaschid
1
550
Docker In Production - A War Story
elbaschid
1
320
Other Decks in Technology
See All in Technology
Sansan Engineering Unit 紹介資料
sansan33
PRO
1
5k
全社に広がるMCPサーバーを、 どう安全に管理するか MCPass開発の舞台裏
mtpooh
3
240
Bet AI Day 2026丨バクラク Autopilot、業務システムの再設計
layerx
PRO
1
810
サーバー常駐型の 簡易障害調査AI エージェントを作ってみた話
masayoshi
1
510
Bet AI Day 2026丨Agentは、「金融」という巨大産業の何を変えられるのか
layerx
PRO
0
540
AI時代に、プロダクトの数だけ積み上がる所有コストをどうエンジニアリングするか / Engineering the Cost of Ownership
kzkmaeda
0
190
DMMブックスのNext.js化を加速させるAI活用 / Migrating DMM Books to Next.js with AI
kentarom
1
300
「ミスを許さない手順書」を作ってみた 〜 個人的にはこれ以上できることはあまりなさそう/20260827-ssmjp-operation-procedure-update
opelab
18
17k
AI駆動開発を組織で促すために
lycorptech_jp
PRO
7
9.2k
Does an AI Watermark Survive Translation?
machinetranslation
0
520
スクラムで身についていた動き方を、XPで捉え直してみた
codmoninc
0
110
Self Healing Rollouts: Automating Production Fixes with Agentic AI
kdubois
0
130
Featured
See All Featured
Technical Leadership for Architectural Decision Making
baasie
3
550
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
810
A better future with KSS
kneath
240
18k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
Product Roadmaps are Hard
iamctodd
55
13k
Amusing Abliteration
ianozsvald
1
280
4 Signs Your Business is Dying
shpigford
187
23k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
800
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Transcript
Hunting for Treasure in Django Sebastian Vetter @elbaschid
Who Am I?
Sebastian • Django & Python Developer • Backend Engineer @
Mobify • github/twitter: elbaschid
What's The Treasure?
Awesome Django Features • Forms, • Views, • Models, •
the ORM, or • other commonly used APIs.
But They Are Boring
Real Treasure
What Does That Mean? • Useful pieces of Django code.
• Considered public API. • Documentation is available (sort of). • Mainly used within Django itself.
My Hunting Strategy • Digging through the Django source. •
Hanging out with Funkybob. • Learning from other great people.
What I'll Do • Show a few "hidden" treasures. •
Explain what they do. • Look at examples.
cached_property
Where is it useful? • Time or compute heavy properties
on a class. • Synchronous calls to remote servers. • Used more than once, e.g. code & template.
What does it do? • It's a decorator. • Caches
the return value. • Lives as long as the instance.
It looks like this class MyObject(object): @cached_property def compute_heavy_method(self): ...
return result
Imagine A Color API class Color(object): def __init__(self, hex): self.hex
= hex def _request_colour_name(self, hex): print "Requesting #{}".format(hex) rsp = requests.get(API_ENDPOINT.format(hex)) return rsp.json()[0].get("title") @property def name(self): return self._request_colour_name(self.hex)
Here's the problem • Using the name attribute will call
the API • Every time!
Here's the problem >>> c = Color('ffffff') >>> c.name Requesting
#ffffff white >>> c.name Requesting #ffffff white
Possible solution @property def name(self): if self._name is None: self._name
= self._request_colour_name(self.hex) return self._name
Or you can use cached_property from django.utils.functional import cached_property @cached_property
def name(self): return self._request_colour_name(self.hex)
Using the cached property >>> c = Color('ffffff') >>> c.name
Requesting #ffffff white >>> c.name white
Isn't That Great
All you Need To Know from django.utils.functional import cached_property •
Only cached for the lifetime of the instance. • Be careful with querysets. • Django docs • Source
import_string
Where is it useful? • Make a class or function
configurable. • Allow loading class/function from string.
What does it do? • Takes dotted path to a
class or function. • Loads it. • Returns the class or function object.
It looks like this from django.utils.module_loading import import_string get_func =
import_string('requests.get') print get_func # <function requests.api.get> get_func('https://google.ca') # <Response [200]>
# settings.py UPLOAD_VALIDATION_PIPELINE = [ 'my_project.uploads.validators.is_tarball', 'my_project.uploads.validators.has_readme_file', 'my_project.uploads.validators.has_no_!']
All you Need To Know from django.utils.module_loading import import_string •
Imports a class or function from a dotted path. • Django docs • Source
lazy and lazy_property
Where is it useful? • Accessing settings at parse time,
e.g. class attributes. • Translating strings outside of a view. • Translations in the settings module.
Here's a problem class UserSignupView(CreateView): ... success_url = reverse('signup-confirmed')
How can we fix it? from django.utils.functional import lazy class
UserSignupView(CreateView): ... success_url = lazy(reverse('signup-confirmed'), unicode)
Lazy Django • The Settings object is lazy. • Several
helpers have lazy siblings: • reverse_lazy • ugettext_lazy • Not sure what lazy_property is useful for.
All you Need To Know from django.utils.functional import lazy from
django.utils.functional import SimpleLazyObject • Imports a class or function from a dotted path. • Django docs • Source
RequestFactory
Where Is It Useful? • Testing request related code. •
Mocking will be too much work. • Using the test client doesn't make sense.
Create GET Request from django.test import RequestFactory request = RequestFactory().get('/some/path')
# with a query string query_params = {"token": "secret-token"} request = RequestFactory().get('/some/path', data=query_params)
Create POST Request from django.test import RequestFactory post_params = {'username':'testuser',
'password':'supersecret'} request = RequestFactory().post('/login', data=post_params)
All you Need To Know from django.test import RequestFactory •
Creates a fake request for given URL. • Can handle all HTTP methods. • Will save you some mocking work. • Django docs • Source
The Treasure Is Yours
Thanks! Questions? • www.roadsi.de • @elbaschid • github.com/elbaschid Slides: https://speakerdeck.com/elbaschid