Skip to content

Commit 7ebe463

Browse files
committed
break out django orm resources
1 parent 9b3bb87 commit 7ebe463

File tree

4 files changed

+148
-80
lines changed

4 files changed

+148
-80
lines changed

content/pages/09-data/06-object-relational-mappers.markdown

Lines changed: 11 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ statement to retrieve every row in the USERS table where the
3030
SELECT * FROM USERS WHERE zip_code=94107;
3131

3232

33-
The equivalent Django ORM query would instead look like the following Python
34-
code:
33+
The equivalent [Django ORM](/django-orm.html) query would instead look like
34+
the following Python code:
3535

3636
# obtain everyone in the 94107 zip code and assign to users variable
3737
users = Users.objects.filter(zip_code=94107)
@@ -135,7 +135,7 @@ There are numerous ORM implementations written in Python, including
135135

136136
1. [SQLAlchemy](/sqlalchemy.html)
137137
1. [Peewee](/peewee.html)
138-
1. [The Django ORM](https://docs.djangoproject.com/en/1.8/topics/db/)
138+
1. [The Django ORM](/django-orm.html)
139139
1. [PonyORM](http://ponyorm.com/)
140140
1. [SQLObject](http://sqlobject.org/)
141141

@@ -147,8 +147,8 @@ ORMs below.
147147

148148
### Django's ORM
149149
The [Django](/django.html) web framework comes with
150-
its own built-in object-relational mapping module, generally referred to
151-
as "the Django ORM" or "Django's ORM".
150+
[its own built-in object-relational mapping module](/django-orm.html),
151+
generally referred to as "the Django ORM" or "Django's ORM".
152152

153153
[Django's ORM](https://docs.djangoproject.com/en/dev/topics/db/) works well
154154
for simple and medium-complexity database operations. However, there are often
@@ -157,11 +157,10 @@ writing straight SQL or using [SQLAlchemy](http://www.sqlalchemy.org/).
157157

158158
It is technically possible to drop down to SQL but it ties the queries to a
159159
specific database implementation. The ORM is coupled closely with Django so
160-
replacing the default ORM with SQLAlchemy is currently a hack workaround. Note
161-
though that some of the Django core committers believe it is only a matter of
162-
time before the default ORM is replaced with SQLAlchemy. It will be a large
163-
effort to get that working though so it's likely to come in
164-
[Django 1.9 or later](https://github.com/mattmakai/fullstackpython.com/issues/48).
160+
replacing the default ORM with SQLAlchemy is currently a hack workaround.
161+
Note though it is possible that swappable ORM backends will be possible
162+
in the future as it is now possible to change the
163+
[template engine](/template-engines.html) for rendering output in Django.
165164

166165
Since the majority of Django projects are tied to the default ORM, it is
167166
best to read up on advanced use cases and tools for doing your best work
@@ -270,75 +269,8 @@ pages.
270269

271270

272271
### Django ORM resources
273-
* [Django models, encapsulation and data integrity](http://www.dabapps.com/blog/django-models-and-encapsulation/)
274-
is a detailed article by Tom Christie on encapsulating Django models for
275-
data integrity.
276-
277-
* [Django Debug Toolbar](http://django-debug-toolbar.readthedocs.org/en/1.2/)
278-
is a powerful Django ORM database query inspection tool. Highly recommended
279-
during development to ensure you're writing reasonable query code.
280-
[Django Silk](http://mtford.co.uk/blog/2/) is another inspection tool and
281-
has capabilities to do more than just SQL inspection.
282-
283-
* [Making a specific Django app faster](http://reinout.vanrees.org/weblog/2014/05/06/making-faster.html)
284-
is a Django performance blog post with some tips on measuring performance
285-
and optimizing based on the measured results.
286-
287-
* [Why I Hate the Django ORM](https://speakerdeck.com/alex/why-i-hate-the-django-orm)
288-
is Alex Gaynor's overview of the bad designs decisions, some of which he
289-
made, while building the Django ORM.
290-
291-
* [Going Beyond Django ORM with Postgres](https://speakerdeck.com/craigkerstiens/going-beyond-django-orm-with-postgres)
292-
is specific to using PostgreSQL with Django.
293-
294-
* [Migrating a Django app from MySQL to PostgreSQL](http://www.calazan.com/migrating-django-app-from-mysql-to-postgresql/)
295-
is a quick look at how to move from MySQL to PostgreSQL. However, my guess
296-
is that any Django app that's been running for awhile on one
297-
[relational database](/databases.html) will require a lot more work to
298-
port over to another backend even with the power of the ORM.
299-
300-
* [Django Model Descriptors](http://blog.kevinastone.com/django-model-descriptors.html)
301-
discusses and shows how to incorporate business logic into Django models
302-
to reduce complexity from the views and make the code easier to reuse across
303-
separate views.
304-
305-
* [Supporting both Django 1.7 and South](http://treyhunner.com/2014/03/migrating-to-django-1-dot-7/)
306-
explains the difficulty of supporting Django 1.7 and maintaining South
307-
migrations for Django 1.6 then goes into how it can be done.
308-
309-
* [Adding basic search to your Django site](https://www.calazan.com/adding-basic-search-to-your-django-site/)
310-
shows how to write generic queries that'll allow you to provide site
311-
search via the Django ORM without relying on another tool like
312-
ElasticSearch. This is great for small sites before you scale them up with
313-
a more robust search engine.
314-
315-
* [How to use Django's Proxy Models](https://www.wellfireinteractive.com/blog/using-django-proxy-models)
316-
is a solid post on a Django ORM concept that doesn't frequently get a lot
317-
of love or explanation.
318-
319-
* [Tightening Django Admin Logins](http://tech.marksblogg.com/django-admin-logins.html)
320-
shows you how to log authentication failures, create an IP addresses white
321-
list and combine fail2ban with the authentication failures list.
322-
323-
* [Django Migrations - a Primer](https://realpython.com/blog/python/django-migrations-a-primer/)
324-
takes you through the new migrations system integrated in the Django core as of Django 1.7, looking specifically at a solid workflow that you can use for creating and applying migrations.
325-
326-
* [Django 1.7: Database Migrations Done Right](https://markusholtermann.eu/2014/09/django-17-database-migrations-done-right/)
327-
explains why South was not directly integrated into Django, how migrations
328-
are built and shows how backwards migrations work.
329-
330-
* [Squashing and optimizing migrations in Django](http://www.rkblog.rk.edu.pl/w/p/squashing-and-optimizing-migrations-django/)
331-
shows a simple example with code for how to use the migrations integrated
332-
into Django 1.7.
333-
334-
* [Sorting querysets with NULLs in Django](https://www.isotoma.com/blog/2015/11/23/sorting-querysets-with-nulls-in-django/)
335-
shows what to do if you're struggling with the common issue of sorting
336-
columns that contain NULL values.
337-
338-
* [Best Practices working with Django models in Python](http://steelkiwi.com/blog/best-practices-working-django-models-python/)
339-
has a ton of great advice on proper model naming conventions, quirks to
340-
avoid with `ForeignKey` field relationships, handling IDs and many other
341-
edge cases that come up when frequently working with Django's ORM.
272+
A curated list of resources can be found on the dedicated
273+
[Django ORM resources](/django-orm.html) page.
342274

343275

344276
### Pony ORM resources
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
title: Django ORM
2+
category: page
3+
slug: django-orm
4+
sortorder: 0909
5+
toc: False
6+
sidebartitle: Django ORM
7+
meta: Django comes with a default object-relational mapping layer for multiple backends in Python web apps.
8+
9+
10+
# Django Object-Relational Mapper (ORM)
11+
The [Django](/django.html) [web framework](/web-frameworks.html) includes
12+
a default
13+
[object-relational mapping layer](/object-relational-mappers-orms.html)
14+
that can be used to interact with data from various
15+
[relational databases](/databases.html) such as [SQLite](/sqlite.html),
16+
[PostgreSQL](/postgresql.html) and [MySQL](/mysql.html).
17+
18+
<a href="https://docs.djangoproject.com/en/dev/topics/db/" style="border: none;"><img src="/img/logos/django.png" width="100%" alt="Official Django logo. Trademark Django Software Foundation." class="technical-diagram"></a>
19+
20+
21+
<div class="well see-also">The Django ORM is an implementation of the <a href="/object-relational-mappers-orms.html">object-relational mapping (ORM)</a> concept. Learn more in the <a href="/data.html">data</a> chapter or view <a href="/table-of-contents.html">all topics</a>.</div>
22+
23+
24+
### Django ORM resources
25+
The Django ORM has evolved over the past dozen years since it was created
26+
make sure to not only read up on the latest tutorials but also learn about
27+
newer optimizations, such as
28+
[prefetch_related](https://docs.djangoproject.com/en/1.11/ref/models/querysets/#prefetch-related)
29+
and
30+
[select_related](https://docs.djangoproject.com/en/1.11/ref/models/querysets/#select-related),
31+
that have been added throughout the project's history.
32+
33+
* [Django models, encapsulation and data integrity](http://www.dabapps.com/blog/django-models-and-encapsulation/)
34+
is a detailed article by Tom Christie on encapsulating Django models for
35+
data integrity.
36+
37+
* [Django Debug Toolbar](http://django-debug-toolbar.readthedocs.org/en/1.8/)
38+
is a powerful Django ORM database query inspection tool. Highly recommended
39+
during development to ensure you're writing reasonable query code.
40+
[Django Silk](http://mtford.co.uk/blog/2/) is another inspection tool and
41+
has capabilities to do more than just SQL inspection.
42+
43+
* [Making a specific Django app faster](http://reinout.vanrees.org/weblog/2014/05/06/making-faster.html)
44+
is a Django performance blog post with some tips on measuring performance
45+
and optimizing based on the measured results.
46+
47+
* [Why I Hate the Django ORM](https://speakerdeck.com/alex/why-i-hate-the-django-orm)
48+
is Alex Gaynor's overview of the bad designs decisions, some of which he
49+
made, while building the Django ORM.
50+
51+
* [Going Beyond Django ORM with Postgres](https://speakerdeck.com/craigkerstiens/going-beyond-django-orm-with-postgres)
52+
is specific to using PostgreSQL with Django.
53+
54+
* [Migrating a Django app from MySQL to PostgreSQL](http://www.calazan.com/migrating-django-app-from-mysql-to-postgresql/)
55+
is a quick look at how to move from MySQL to PostgreSQL. However, my guess
56+
is that any Django app that's been running for awhile on one
57+
[relational database](/databases.html) will require a lot more work to
58+
port over to another backend even with the power of the ORM.
59+
60+
* [Django Model Descriptors](http://blog.kevinastone.com/django-model-descriptors.html)
61+
discusses and shows how to incorporate business logic into Django models
62+
to reduce complexity from the views and make the code easier to reuse across
63+
separate views.
64+
65+
* [Adding basic search to your Django site](https://www.calazan.com/adding-basic-search-to-your-django-site/)
66+
shows how to write generic queries that'll allow you to provide site
67+
search via the Django ORM without relying on another tool like
68+
ElasticSearch. This is great for small sites before you scale them up with
69+
a more robust search engine.
70+
71+
* [How to use Django's Proxy Models](http://benlopatin.com/using-django-proxy-models/)
72+
is a solid post on a Django ORM concept that doesn't frequently get a lot
73+
of love or explanation.
74+
75+
* [Tightening Django Admin Logins](http://tech.marksblogg.com/django-admin-logins.html)
76+
shows you how to log authentication failures, create an IP addresses white
77+
list and combine fail2ban with the authentication failures list.
78+
79+
* [Sorting querysets with NULLs in Django](https://www.isotoma.com/blog/2015/11/23/sorting-querysets-with-nulls-in-django/)
80+
shows what to do if you're struggling with the common issue of sorting
81+
columns that contain NULL values.
82+
83+
* [Best Practices working with Django models in Python](http://steelkiwi.com/blog/best-practices-working-django-models-python/)
84+
has a ton of great advice on proper model naming conventions, quirks to
85+
avoid with `ForeignKey` field relationships, handling IDs and many other
86+
edge cases that come up when frequently working with Django's ORM.
87+
88+
* [Solving performance problems in the Django ORM](https://medium.com/@hansonkd/performance-problems-in-the-django-orm-1f62b3d04785)
89+
gives a slew of great code snippets to use with `django.db.connection` so
90+
you can discover issues such as unexpected extra queries and problematic
91+
key relationships.
92+
93+
94+
### Django migrations resources
95+
[Django migrations](https://docs.djangoproject.com/en/dev/topics/migrations/)
96+
were added in
97+
[version 1.7](https://docs.djangoproject.com/en/dev/releases/1.7/). Django
98+
projects prior to 1.7 used the
99+
[South project](https://south.readthedocs.io/en/latest/), which is now
100+
deprecated and merged into Django. Migrations can be tricky to wrap your
101+
head around as you're getting started with the overall framework but the
102+
following resources should get you past the initial hurdles.
103+
104+
* [Django Migrations - a Primer](https://realpython.com/blog/python/django-migrations-a-primer/)
105+
takes you through the new migrations system integrated in the Django core as of Django 1.7, looking specifically at a solid workflow that you can use for creating and applying migrations.
106+
107+
* [Django 1.7: Database Migrations Done Right](https://markusholtermann.eu/2014/09/django-17-database-migrations-done-right/)
108+
explains why South was not directly integrated into Django, how migrations
109+
are built and shows how backwards migrations work.
110+
111+
* [Squashing and optimizing migrations in Django](http://www.rkblog.rk.edu.pl/w/p/squashing-and-optimizing-migrations-django/)
112+
shows a simple example with code for how to use the migrations integrated
113+
into Django 1.7.
114+
115+
* [Supporting both Django 1.7 and South](http://treyhunner.com/2014/03/migrating-to-django-1-dot-7/)
116+
explains the difficulty of supporting Django 1.7 and maintaining South
117+
migrations for Django 1.6 then goes into how it can be done.
118+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<h3>What would you like to learn about after the Django ORM?</h3>
2+
<div class="row">
3+
<div class="col-md-4">
4+
<div class="well select-next">
5+
{% include "choices/buttons/databases.html" %}
6+
</div>
7+
</div>
8+
<div class="col-md-4">
9+
<div class="well select-next">
10+
{% include "choices/buttons/no-sql-datastore.html" %}
11+
</div>
12+
</div>
13+
<div class="col-md-4">
14+
<div class="well select-next">
15+
{% include "choices/buttons/data.html" %}
16+
</div>
17+
</div>
18+
</div>

theme/templates/table-of-contents.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ <h4 class="toc-subsection">9.1 <a href="/databases.html">Relational databases</a
249249
<h4 class="toc-subsection">9.2 <a href="/object-relational-mappers-orms.html">Object-relational mappers</a></h4>
250250
<div class="toc"><a href="/sqlalchemy.html">SQLAlchemy</a></div>
251251
<div class="toc"><a href="/peewee.html">Peewee</a></div>
252-
<div class="toc soon">Django ORM</div>
252+
<div class="toc"><a href="/django-orm.html">Django ORM</a></div>
253253
<div class="toc soon">SQLObject</div>
254254
<div class="toc soon">Pony ORM</div>
255255

0 commit comments

Comments
 (0)