Today I Learned - Django Queryset Default Ordering Is No Ordering - by Rui Rei - Medium

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Rui Rei 12 Followers About Follow Sign in Get started

Sign in to your account (ok__@g__.com) for your personalized experience.

Sign in with Google Not you? Sign in or create an account

Today I Learned: Django


queryset default ordering is no
ordering
Rui Rei Sep 9 · 1 min read

Just so other people don’t make the same mistake as I did, I simply want to
emphasize that the order of a queryset that does not contain an explicit
.order_by() clause (and its model doesn’t have a default ordering defined
in its Meta ) is database-dependent. In other words, it’s arbitrary and can’t
be relied upon.

Although this is properly detailed in the Django docs (picture below), I


somehow managed to miss it and wrongly assumed that the elements
would be returned in PK order. If your code depends on the order of a
queryset’s elements, you must either use an explicit .order_by() clause or
add an ordering option to the model’s Meta . I strongly advise against the

latter though, as it may reduce readability and violates the principle of least
astonishment (because the ordering is “hidden” in a different location from
where the queryset is created).

Django Django Queryset Python

More from Rui Rei Follow

Jul 28, 2020

A tale of high memory usage in Django

Introduction
I was writing a script that iterates over a relatively large queryset (with a
few million entries) and makes a few simple changes to each model
instance. A very simplified version of what I’m trying to accomplish:

1 def do_stuff(obj):
2 """Performs some simple processing on `obj`."""
3 obj.y = obj.related_obj.x ** 2 + 1 # DB query to fetch `.related_obj`
4 obj.save() # DB query to update `obj`
5
6
7 # Obtain a large queryset for this example.
8 qs = MyFurstModel.objects.all()
9
10 # Simply iterate over the queryset and do some processing on each element.
11 # Sounds simple enough, right? WRONG!
12 for obj in qs:
13 do_stuff(obj)

problem.py hosted with ❤ by GitHub view raw

Other than the size of the queryset, this is all very plain…

Read more · 3 min read

12

Share your ideas with millions of readers. Write on Medium

Jun 29, 2020

Today I Learned: resources for NGINX


content caching
This is just a list of links to the documentation explaining how to cache
upstream content using an NGINX reverse proxy.

(guide) https://docs.nginx.com/nginx/admin-guide/content-
cache/content-caching/

(guide) https://www.nginx.com/blog/nginx-caching-guide/

(reference) https://nginx.org/en/docs/http/ngx_http_proxy_module.html

(example)
https://www.nginx.com/resources/wiki/start/topics/examples/reversepro
xycachingexample/

(CAVEAT)https://stackoverflow.com/questions/9230812/nginx-as-cache-
proxy-not-caching-anything

May 28, 2020

Today I Learned: great, quick article on


nginx configuration inheritance
https://blog.martinfjordvald.com/understanding-the-nginx-configuration-
inheritance-model/

May 27, 2020

Today I Learned: proper glob matching in


Python with Wildcard Match

The Wildcard Match (PyPI distribution name wcmatch ) library proposes to

replace the glob-related functionality present in the standard library with a


library that exposes similar APIs but adds many missing features (notably,
GLOBSTAR **). By the words in the official website at
https://facelessuser.github.io/wcmatch/

Wildcard Match provides an enhanced fnmatch , glob , and pathlib library in


order to provide file matching and globbing that more closely follows the
features found in Bash.

The library is well documented, behaves as expected, and I love it :)

Here is the link to the GitHub repository —


https://github.com/facelessuser/wcmatch

May 25, 2020

Today I Learned: a selection of Python CLI


libraries

Quick and easy:

http://docopt.org/ (stunning API design, no dependencies)

https://github.com/google/python-fire (CLI-ze all the things!)

Feature-packed:

https://click.palletsprojects.com/en/7.x/ (great for larger projects)

https://github.com/tiangolo/typer (extends click to make things even


easier)

More From Medium

How to monitor 0–16V DC QA Software Tester CV Launch and Manage EC2 Add CSS to Blogger
Battery through Example: How to Build a Instances Using AWS CLI PC — HUB
Smartphone — Ideal for Job Seeking Resume Arohan Ajit in The Startup
Car Batteries and Solar TestDel
Batteries.
Felix B Daughtery

Read before DfiStarter Microcontroller 101 A few Dotty things going Exciting Features of Snap
product test Hai Nguyen
on Camera
DfiStarter Mark "Justin" Waks Pranjal Saxena in Heartbeat

About Write Help Legal

You might also like