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
Django and PostgreSQL, a happy relationship
Search
Iacopo Spalletti
October 23, 2015
Programming
0
32
Django and PostgreSQL, a happy relationship
Given at PgDay Italy 2015 23 October 2015
Iacopo Spalletti
October 23, 2015
Tweet
Share
More Decks by Iacopo Spalletti
See All by Iacopo Spalletti
Writing Async Microservices in Python
yakky
0
550
1 API - 3 Framework - 30 minutes
yakky
0
91
Building real time applications with Django and Channels 2 @ DjangoCon Europe
yakky
1
570
Building real time applications with Django and Channels 2 @ PyCon Italia
yakky
0
420
Building real time applications with Django
yakky
0
530
django knocker
yakky
0
55
django CMS application - A comprehensive approach
yakky
0
46
django CMS + Channels + DRF = ♥
yakky
0
310
Liveblogging using channels
yakky
0
39
Other Decks in Programming
See All in Programming
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
230
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
270
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
160
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
120
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
5
900
Security_for_introducing_eBPF
kentatada
0
110
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
useSyncExternalStoreを使いまくる
ssssota
6
1k
Featured
See All Featured
Speed Design
sergeychernyshev
25
670
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
For a Future-Friendly Web
brad_frost
175
9.4k
Rails Girls Zürich Keynote
gr2m
94
13k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Designing for humans not robots
tammielis
250
25k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
RailsConf 2023
tenderlove
29
940
Transcript
PGDAY 2015 DJANGO AND POSTGRESQL, A HAPPY RELATIONSHIP by Iacopo
Spalletti @yakkys
WHO AM I? Founder and Lead developer @NephilaIt django CMS
core developer django CMS installer author
WHAT'S DJANGO? A web framework written in Python Based on
an DBMS-agnostic ORM
DJANGO ORM But sometimes abstraction is not enough
POSTGRESQL EXTENSIONS PostGIS Support django-hstore django.contrib.postgres
DJANGO-HSTORE External application to support hstore datatype Supports Django 1.6-1.8+
Integrated editor
DJANGO-HSTORE Easy to use class PrinterData(models.Model): printer = models.ForeignKey(Printer) data
= hstore.DictionaryField() ... objects = hstore.HStoreManager() That's all (plus run create extension hstore)
DJANGO-HSTORE Demo time
CONTRIB.POSTGRES Official module Available since 1.8 Supports many datatypes UUID
HStore Array Rangefields
CONTRIB.POSTGRES UUID class Example(models.Model): id = models.UUIDField(default=uuid.uuid4, primary_key=True)
CONTRIB.POSTGRES ARRAYFIELD class Post(models.Model): name = models.CharField(max_length=200) tags = ArrayField(models.CharField(max_length=200))
Supports query functions: contains (@>), contained_by (<@), overlap (&&), len, indexing, slicing
CONTRIB.POSTGRES ARRAYFIELD # SQL operator: @> Post.objects.filter(tags__contains=['thoughts']) # SQL operator:
<@ Post.objects.filter(tags__contained_by=['thoughts', 'django']) # SQL operator: && Post.objects.filter(tags__overlap=['thoughts']) # len Post.objects.filter(tags__len=1) # indexing Post.objects.filter(tags__1__iexact='django') # slicing Post.objects.filter(tags__1_2='thoughts')
CONTRIB.POSTGRES HSTORE class Post(models.Model): name = models.CharField(max_length=200) data = HStoreField()
Support query functions: filter, chaining, contains, contained_by, has_key, has_any_key, has_keys
CONTRIB.POSTGRES HSTORE # filter Post.objects.filter(data__category='main') # chaining Post.objects.filter(data__category__contains='h') # SQL
operator: @> Post.objects.filter(data__contains={'category': 'home'}) # SQL operator: <@ Post.objects.filter(data__contained_by={'category': 'home', 'author' # SQL operator: ? Post.objects.filter(data__has_key='category') # SQL operator: ?| Post.objects.filter(data__has_any_keys='category') # SQL operator: ?& Post.objects.filter(data__has_keys=['category', 'author'])
CONTRIB.POSTGRES RANGE FIELDS python_2_unicode_compatible class Post(models.Model): name = models.CharField(max_length=200) ...
skill_range = IntegerRangeField() Supports a lot of query functions: contains, contained_by, overlap, fully_lt, fully_gt, not_lt, not_gt, adjacent_to, startswith, endswith, isempty Definition of custom range types
CONTRIB.POSTGRES RANGE FIELDS # SQL operator: @> Post.objects.filter(skill_range__contains=NumericRange(3, 5)) #
SQL operator: <@ Post.objects.filter(skill_range__contained_by=NumericRange(2, 3)) # SQL operator: && Post.objects.filter(skill_range__overlap=NumericRange(2, 3)) # fully_lt Post.objects.filter(skill_range__fully_lt=NumericRange(3, 6)) # fully_gt Post.objects.filter(skill_range__fully_gt=NumericRange(2, 3))
DJANGO AND POSTGRESQL Support is evolving More goodies in 1.9
GRAZIE!