Professional Documents
Culture Documents
Today I Learned - Django Queryset Default Ordering Is No Ordering - by Rui Rei - Medium
Today I Learned - Django Queryset Default Ordering Is No Ordering - by Rui Rei - Medium
Today I Learned - Django Queryset Default Ordering Is No Ordering - by Rui Rei - Medium
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.
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).
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)
Other than the size of the queryset, this is all very plain…
12
(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
Feature-packed:
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