News for hackers. Stuff that matters.
Re:Literate SQL by 3131s on Sunday June 25, 2017 @01:01PM (#14630948)
Write raw SQL queries. My feeling on the Django ORM is that it's useful for defining models and managing migrations, but I prefer to write raw SQL for all queries.

The PostgreSQL documentation is really good too.

Re:Literate SQL by ubernostrum on Monday June 26, 2017 @01:00AM (#14634203)
Meh. My feeling is that Django and other web frameworks are too magical -- I always worry they might be doing something inefficient. That's why I write all my web applications in hand-tuned assembly. It even has reusable abstractions for when I need them, and nice hand-formatted assembly is super readable!
Re:Literate SQL by 3131s on Monday June 26, 2017 @10:31AM (#14637077)
You're seriously comparing SQL to assembly? There's a point where it doesn't make sense to add another layer, another dependency, etc.
Re:Literate SQL by ubernostrum on Monday June 26, 2017 @11:09AM (#14637391)
In both the "never use an ORM" and "always use assembly" cases, the argument is based entirely on having full control of what's going on and not trusting any intermediate layer to get it right or eke out every last femtosecond worth of performance.

So it's a perfectly fair comparison.

Re:Literate SQL by collyw on Sunday June 25, 2017 @01:09PM (#14631002)
The ORM takes a whole lot of pain out of updates as well.

Filtering also work fairly nicely for simple stuff, as putting conditions in a dictionary and passing it to filter(kwargs) is a lot nicer than messing about with SQL strings.

I agree that the ORM is limited if you want moderately complex queries though.

Re:Literate SQL by Daishiman on Sunday June 25, 2017 @04:56PM (#14632131)
> but I prefer to write raw SQL for all queries.

If you're doing this you're missing out on the biggest advantage of the ORM, which is that it allows you to compose queries.

Using the Q objects, query expressions, and custom Queryset objects, you can filter objects by pretty much every imaginable criteria out there. This would be horrendously difficult and error-prone writing plain SQL.

The Django ORM, despite having limitations if you want to run analytics (and that's really a use case for with plain SQL excels), generally writes out exactly the same SQL I'd write by hand.

Re:Literate SQL by 3131s on Sunday June 25, 2017 @11:09PM (#14633780)
> This would be horrendously difficult and error-prone writing plain SQL.

I do it all the time :) I love looking at long SQL queries that are formatted nicely and use good naming conventions.

It's also part of a more general philosophy I have of reducing dependencies whenever possible. With Django it makes sense to use the ORM wherever it suits you, since it's already built in, but there are times when you need to know raw SQL anyway (e.g. try populating a large database without COPY, only using the Django ORM).